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 只支持纯文本级别的样式控 ...
随机推荐
- ES7/8新特性学习随笔
随着每年EcmaScript都会为js带来一些新特性,带来更多美化的编程体验,今天就走进一下es2016/2017所带来的新特性 ES7新特性 includes() 指数操作符 ES8新特性 asyn ...
- 以OPC PowerTool 连接iFix与KEPWARE
1.安装完iFix后,再安装KEPWARE软件,然后必须再安装所需要的IO驱动才能进行device的通讯连接.这里安装iFix本身提供的OPC PowerTool V7.34a. 2.在安装完iFix ...
- JavaScript获取事件对象和目标对象
在JavaScript开发中,经常需要获取触发某个事件的目标对象.让后根据目标对象进行不同的业务处理.下面展示通过JavaScript获取触发事件的事件目标对象.如下: Js代码 1 2 3 4 5 ...
- LPC-Link2 CMSIS-DAP firmware source
LPC-Link2 CMSIS-DAP firmware source Hi, I'm using the CMSIS-DAP firmware with the LPC-Link2. I'd lik ...
- C#访问远程主机资源的方法,多种方式
最近要实现访问远程主机的共享目录中的一个文件.遇到了权限问题.google了一下,找到了几种解决方法,记录如下: 一.调用Net use命令 // 使用方法: //if (Connect ...
- java之 ------ 文件拷贝
import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.FileOutputStrea ...
- 基于设备树的TQ2440 DMA学习(1)—— 芯片手册
作者 彭东林pengdonglin137@163.com 平台 TQ2440内核Linux4.9 概述 一直想抽时间学习一下DMA驱动,今天就以S3C2440为例,这款芯片的DMA控制器足够简单,也比 ...
- Unity3D实践系列05,为GameObject添加额外属性
在Unity中,通常通过脚本为GameObject添加额外的属性.具体有2种方式:一种是通过硬编码为脚本字段赋值,另一种是通过反射在运行时给脚本字段赋值. 脚本通过字段硬编码为GameObject添加 ...
- springboot之异步调用@Async
原文:http://www.cnblogs.com/xuwenjin/p/8858050.html 引言: 在Java应用中,绝大多数情况下都是通过同步的方式来实现交互处理的:但是在处理与第三方系统交 ...
- Mybatis配置返回为修改影响条数
mybatis配置返回为修改影响条数,修改jdbc连接如下即可:添加useAffectedRows=true配置. jdbc:mysql://jdbc.host/{jdbc.db}?useAffect ...