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
复制代码

 

四、实现

复制代码
1)创建

- (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];
}

 (2)显示

-(void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];

    //hide system navigationBar
    [self.navigationController setNavigationBarHidden:YES animated:YES];
    
    //show custom navigationBar
    [self.navigationBar showCustomNavigationBar:YES];
}

 (3)隐藏

-(void)viewWillDisappear:(BOOL)animated{
    [super viewWillDisappear:animated];

    //show system navigationBar
    [self.navigationController setNavigationBarHidden:NO animated:YES];

    //hide custom navigationBar
    [self.navigationBar showCustomNavigationBar:NO];
}

 (4)监测

#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 > 1 ? 1 : 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

 

 

程序猿神奇的手,每时每刻,这双手都在改变着世界的交互方式!
本文转自当天真遇到现实博客园博客,原文链接:http://www.cnblogs.com/XYQ-208910/p/7453908.html ,如需转载请自行联系原作者
相关文章
|
4天前
|
iOS开发 UED
实现一个自定义的iOS动画效果
【4月更文挑战第9天】本文将详细介绍如何在iOS平台上实现一个自定义的动画效果。我们将通过使用Core Animation框架来实现这个动画效果,并展示如何在不同的场景中使用它。文章的目标是帮助读者理解如何使用Core Animation框架来创建自定义动画,并提供一个简单的示例代码。
19 1
|
8月前
|
iOS开发
iOS多线程之NSOperationQueue-依赖、并发数、优先级、自定义Operation等最全的使用总结
iOS多线程之NSOperationQueue-依赖、并发数、优先级、自定义Operation等最全的使用总结
236 0
|
8月前
|
iOS开发
iOS 多个滚动控件嵌套Demo
iOS 多个滚动控件嵌套Demo
41 0
|
8月前
|
API iOS开发
iOS 自定义转场动画 UIViewControllerTransitioning
iOS 自定义转场动画 UIViewControllerTransitioning
53 0
|
9月前
|
Swift iOS开发
iOS 13 之后自定义 Window 不显示解决 (SceneDelegate)
iOS 13 之后自定义 Window 不显示解决 (SceneDelegate)
265 0
|
Linux iOS开发 开发者
WIN11自定义版本ios镜像下载教程
WIN11自定义版本ios镜像下载教程
WIN11自定义版本ios镜像下载教程
|
API iOS开发 Perl
iOS UISlider自定义渐变色滑杆
iOS UISlider自定义渐变色滑杆
iOS UISlider自定义渐变色滑杆
|
API iOS开发 Perl
iOS UILabel自定义位置
iOS UILabel自定义位置
iOS UILabel自定义位置
iOS--设置系统导航栏右上角按钮不显示问题
iOS--设置系统导航栏右上角按钮不显示问题
200 0
|
开发工具 iOS开发 git
iOS开发 - 类似美团选商品页,从按钮上往上滑动,tableview依然响应,点击按钮,按钮也可响应
iOS开发 - 类似美团选商品页,从按钮上往上滑动,tableview依然响应,点击按钮,按钮也可响应
178 0
iOS开发 - 类似美团选商品页,从按钮上往上滑动,tableview依然响应,点击按钮,按钮也可响应