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 只支持纯文本级别的样式控 ...
随机推荐
- [代码审计]covercms 后台getshell
0x00 环境介绍 CMS名称: covercms 运行环境: php 5.6.27-nts + apache + mysql 系统版本: 1.16 漏洞等级:高危 漏洞简介: 后台awnotas.i ...
- UICollectionView 常用操作
1 iOS开发 - UICollectionView点击展开收起
- python语法(四)— 文件操作
前面几天学习了一写python的基础语法,也学习了分支if,循环while和for.由于之前已经做过几年的开发了,所以我们知道,许多数据来源并不是靠键盘输入到程序中去的,而是通过数据库和文件来获取到的 ...
- Codeforces.1027F.Session in BSU(思路 并查集)
题目链接 \(Description\) 有\(n\)个人都要参加考试,每个人可以在\(ai\)或\(bi\)天考试,同一天不能有两个人考试.求最晚考试的人的时间最早能是多少.无解输出-1. \(So ...
- BZOJ.2440.[中山市选2011]完全平方数(莫比乌斯函数 二分)
题目链接 总感觉博客园的\(Markdown\)很..\(gouzhi\),可以看这的. 题意即求第\(k\)个无平方因子数. 无平方因子数(Square-Free Number),即分解之后所有质因 ...
- 【学习笔记】python的代码块(吐槽)
曾经我以为python是像pascal那样begin开始end结束, 直到今天…… 我才知道python是用缩进作为代码段标识的…… >>> def test(n): ... if ...
- Linux学习笔记05—文件与目录权限
1. 绝对路径与相对路径绝对路径:路径的写法一定由根目录 ‘/’写起,例如 /usr/local/mysql 这就是绝对路径相对路径:路径的写法不是由根目录 ‘/’写起,例如:首先用户进入到/, 然后 ...
- STM32F2x Is it possible to request multiple DMA streams with single request
I want to setup an application, where a single trigger-factor (compare-match of a timer) shall reque ...
- 基于Linux的智能家居的设计(3)
2 硬件设计 本课题的硬件设计包含主控制器.传输数据设计.数据採集设计.控制驱动设计.显示设计.门禁设计. 2.1 主控制器 依据方案三选择S3C6410主控芯片,S3C6410是由Samsung ...
- Snmp学习总结系列——开篇
进入公司以来,一直参与到公司的产品研发工作当中去,在产品研发中有一个监控远程服务器CPU使用率,内存使用情况,硬盘的需求,技术总监提出了使用Snmp协议作为远程监控的技术解决方案,头一次听说Snmp这 ...