iOS:自定义导航栏,随着tableView滚动显示和隐藏
自定义导航栏,随着tableView滚动显示和隐藏
一、介绍
自定义导航栏是APP中很常用的一个功能,通过自定义可以灵活的实现动画隐藏和显示效果。虽然处理系统的导航栏也可以实现,但是这个是有弊端的,因为系统导航栏是全局的,在任何一个地方去修改导航栏内部的结构,其他地方都会改变,需要再次去特殊处理,否则很容易出现不可预知的bug。此时,自定义是最好的选择。
二、思想
(1)在控制器将要显示时,隐藏系统的导航栏,显示自定义的导航栏
(2)在控制器将要消失时,显示系统的导航栏,隐藏自定义的导航栏
(3)重写scrollView的代理方法,监测ContentOffst.y偏移,动态控制自定义的导航栏的可见性
三、定义
@interface XYQNavigationBar : UIView
@property (copy , nonatomic)void(^clickLeftItemBlock)(); // click left button block
@property (copy , nonatomic)void(^clickRightItemBlock)(); // click right button block
@property (copy , nonatomic)NSString *title; // title
@property (assign,nonatomic)UIColor *titleColor; // title color
@property (strong,nonatomic)UIColor *navBackgroundColor;// navagationBar background color
@property (strong,nonatomic)UIImage *navBackgroundImage;// navigationBar background image
+(instancetype)createCustomNavigationBar; //create navigationBar
-(void)showCustomNavigationBar:(BOOL)show; //set navigationBar hide, defalut is NO
-(void)setupBgImageAlpha:(CGFloat)alpha animation:(NSTimeInterval)duration compeleteBlock:(void (^)())compeleteBlock;// navigationBar background image alpha
-(void)setupBgColorAlpha:(CGFloat)alpha animation:(NSTimeInterval)duration compeleteBlock:(void (^)())compeleteBlock;// navigationBar background color alpha
-(void)setLftItemImage:(NSString *)imgName leftItemtitle:(NSString *)leftItemtitle textColor:(UIColor *)color; // navigationBar left button has title and image
-(void)setRightItemImage:(NSString *)imgName rightItemtitle:(NSString *)rightItemtitle textColor:(UIColor *)color;// navigationBar right button has title and image
-(void)setLeftItemImage:(NSString *)imgName; // navigationBar left button only has image
-(void)setRightItemImage:(NSString *)imgName; // navigationBar right button only has image
@end
四、实现
()创建
- (void)viewDidLoad {
[super viewDidLoad];
//init
self.tableView = [[UITableView alloc] initWithFrame:self.view.bounds];
self.tableView.dataSource = self;
self.tableView.delegate = self;
self.navigationBar = [XYQNavigationBar createCustomNavigationBar];
//update(此处可以自由改变导航栏的属性值)
self.navigationBar.title = @"自定义导航栏";
//block
__weak typeof(self) weakSelf = self;
self.navigationBar.clickLeftItemBlock = ^(){
[weakSelf.navigationController popViewControllerAnimated:YES];
};
self.navigationBar.clickRightItemBlock = ^(){
[weakSelf.navigationController pushViewController:[[SecondViewController alloc] init] animated:YES];
};
// add
[self.view addSubview:self.tableView];
[self.view addSubview:self.navigationBar];
}
()显示
-(void)viewWillAppear:(BOOL)animated{
[super viewWillAppear:animated];
//hide system navigationBar
[self.navigationController setNavigationBarHidden:YES animated:YES];
//show custom navigationBar
[self.navigationBar showCustomNavigationBar:YES];
}
()隐藏
-(void)viewWillDisappear:(BOOL)animated{
[super viewWillDisappear:animated];
//show system navigationBar
[self.navigationController setNavigationBarHidden:NO animated:YES];
//hide custom navigationBar
[self.navigationBar showCustomNavigationBar:NO];
}
()监测
#pragma mark - public methods
// animation show or hide navigationbar
-(void)scrollViewDidScroll:(UIScrollView *)scrollView{
CGFloat offsetY = scrollView.contentOffset.y;
CGFloat colorOffset = offsetY / 64.0;
colorOffset = colorOffset > ? : colorOffset;
//method 1 : change backgrundViewImage alpha
[self.navigationBar setupBgImageAlpha:colorOffset animation:0.4 compeleteBlock:nil];
//method 2 : change backgrundViewColor alpha
//[self.navigationBar setupBgColorAlpha:colorOffset animation:0.4 compeleteBlock:nil];
}
五、效果

六、下载
github:https://github.com/xiayuanquan/XYQNavigationBar.git
iOS:自定义导航栏,随着tableView滚动显示和隐藏的更多相关文章
- iOS 自定义导航栏笔记
一.UINavigationBar的结构 导航栏几乎是每个页面都会碰到的问题,一般两种处理方式:1.隐藏掉不显示 2.自定义 1. 添加导航栏 TestViewController * mainVC ...
- iOS 自定义导航栏 和状态栏
一.更改状态栏颜色 (StatusBar) 就是比如导航栏是红色的状态栏是绿色的. 要实现这样的效果其实很简单,就是添加一个背景view. 简单的实现过程如下: 1 // 设置导航颜色 可用 2 [s ...
- ios 自定义导航栏,开启侧滑返回手势
自定义一个常用ListViewController .h文件 #import <UIKit/UIKit.h> @interface ListViewController : UIViewC ...
- IOS 自定义导航栏背景
//- (void)setBackgroundImage:(UIImage *)backgroundImage forBarMetrics:(UIBarMetrics)barMetrics NS_AV ...
- iOS 自定义导航栏
参考链接:https://blog.csdn.net/huanglinxiao/article/details/100537988 demo下载地址:https://github.com/huangx ...
- iOS 11 导航栏 item 偏移问题 和 Swift 下 UIButton 设置 title、image 显示问题
html,body,div,span,applet,object,iframe,h1,h2,h3,h4,h5,h6,p,blockquote,pre,a,abbr,acronym,address,bi ...
- [RN] React Native 自定义导航栏随滚动渐变
React Native 自定义导航栏随滚动渐变 实现效果预览: 代码实现: 1.定义导航栏 NavPage.js import React, {Component} from 'react'; im ...
- iOS个人中心渐变动画、微信对话框、标签选择器、自定义导航栏、短信验证输入框等源码
iOS精选源码 简单的个人中心页面-自定义导航栏并予以渐变动画 程序员取悦女票的正确姿势---Tip1(iOS美容篇) iOS 前台重启应用和清除角标的问题 微信原生提醒对话框3.0 JHLikeBu ...
- 微信小程序自定义导航栏组件,完美适配所有手机,可实现各种功能和情况
背景 在做小程序时,关于默认导航栏,我们遇到了以下的问题: Android.IOS 手机对于页面 title 的展示不一致,安卓 title 的显示不居中 页面的 title 只支持纯文本级别的样式控 ...
随机推荐
- map的基本操作函数
C++ maps是一种关联式容器,包含“关键字/值”对 begin() 返回指向map头部的迭代器 clear() 删除所有元素 count() 返回指定 ...
- 10BASE
10BASE-T,10BASE-5,10BASE-2,以太网的技术标准,10Base-2.10Base-5.10Base-T都是以太网的技术标准,传输速率为10Mbps. 10Base-2技术以细 ...
- Gift动图分解小工具
gif 动图分解小工具 Overview 因为自己有时候需要将一些gif图片分解,但是没有在网上找到合适的工具,所有就自己写了一个,在这里与大家分享,其实实现很简单,是通过C#实现的.文章下方有下载链 ...
- hdu 4336 概率dp
题意:有N(1<=N<=20)张卡片,每包中含有这些卡片的概率为p1,p2,````pN.每包至多一张卡片,可能没有卡片.求需要买多少包才能拿到所以的N张卡片,求次数的期望. 转移方程: ...
- BZOJ3355 : [Usaco2004 Jan]有序奶牛
对于一条边x->y,若去掉之后x不能到达y,那么它是必需的. 首先拓扑排序求出拓扑序,然后按照终点拓扑序为第一关键字,起点拓扑序为第二关键字从小到大加边. 对于每个点,维护一个bitset,表示 ...
- 【BZOJ-1913】signaling信号覆盖 极角排序 + 组合
1913: [Apio2010]signaling 信号覆盖 Time Limit: 20 Sec Memory Limit: 64 MBSubmit: 1232 Solved: 506[Subm ...
- object-c中NSString与int和float的相互转换
1,字符串拼接 NSString *newString = [NSString stringWithFormat:@"%@%@",tempA,tempB]; 2,字符转int in ...
- 允许mysql远程用户连接。
默认mysql是禁止远程用户连接的.连接提示: 1045,“Access denied for user 'root'@'192.168.100.1' (using password:YES)&quo ...
- CentOS下使用autoenv实现进入特定目录后运行特定环境变量
安装 # Mac git clone git://github.com/kennethreitz/autoenv.git ~/.autoenv # 安装默认shell echo 'source ~/. ...
- STM32 F4 DAC DMA Waveform Generator
STM32 F4 DAC DMA Waveform Generator Goal: generating an arbitrary periodic waveform using a DAC with ...