美文网首页iOS开发iOS开发iOS技术资料
iOS自定义导航栏-导航栏联动(二)

iOS自定义导航栏-导航栏联动(二)

作者: QuintGao | 来源:发表于2017-07-19 13:12 被阅读535次

    iOS自定义导航栏-导航栏联动(一)
    iOS自定义导航栏-导航栏联动(二)

    前言

    最近通过对一些APP的观察发现,现在绝大多数的APP都会有导航栏联动效果即:返回时导航栏会随着控制器一起返回,那么我们应该如何来处理这种情况呢,经过研究及查找资料发现有以下两种方式
    隐藏掉系统的导航栏,为每一个控制器添加自定义导航栏,但是这种方法有个弊端就是不能够使用系统导航栏的属性了
    通过一些特殊处理为每个控制器包装一个导航控制器,但由于导航控制器的子类不能是导航控制器,所以需要再包装一个控制器

    GKNavigationBarViewController使用的是第一种方法,下面具体说一下实现方法及用法。

    一、框架特性

    * 支持自定义导航栏样式(隐藏、透明等)
    * 支持控制器开关返回手势
    * 支持控制器开关全屏返回手势
    * 支持控制器设置导航栏透明度,可实现渐变效果
    * 完美解决UITableView,UIScrollView滑动手势冲突
    * 可实现push,pop时控制器缩放效果(如:今日头条)
    * 可实现左滑push一个控制器的效果(如:网易新闻)
    

    Demo中部分截图如下

    今日头条.gif 网易云音乐.gif 网易新闻.gif

    使用说明

    1、今日头条的实现

    UINavigationController作为根控制器,包含一个UITabBarController,UITabBarController中包含以GKNavigationBarViewController为父类的子类

    导航栏创建方式

    GKToutiaoViewController *toutiaoVC = [GKToutiaoViewController new];
    
    // 根控制器是导航控制器,需要缩放
    UINavigationController *nav = [UINavigationController rootVC:toutiaoVC translationScale:YES];
    
    

    2、网易云音乐的实现

    UITabBarController作为根控制器,包含带导航栏的以GKNavigationBarViewController为父类的子类

    3、网易新闻的实现

    UITabBarController作为根控制器,包含带导航栏的以GKNavigationBarViewController为父类的子类
    其中导航栏开启左滑push手势,主要代码如下:

    // 导航栏开启左滑push
    UINavigationController *nav = [UINavigationController rootVC:vc translationScale:NO];
    nav.gk_openScrollLeftPush = YES;
    
    // 单个控制器中设置左滑push代理,并实现方法
    1. // 设置push的代理
    self.gk_pushDelegate = self;
    
    2. 实现代理方法
    - (void)pushToNextViewController {
        GKWYNewsCommentViewController *detailVC = [GKWYNewsCommentViewController new];
        detailVC.hidesBottomBarWhenPushed = YES;
        [self.navigationController pushViewController:detailVC animated:YES];
    }
    
    

    4、部分属性介绍

    UINavigationController

    /** 导航栏转场时是否缩放,此属性只能在初始化导航栏的时候有效,在其他地方设置会导致错乱 */
    @property (nonatomic, assign, readonly) BOOL gk_translationScale;
    
    /** 是否开启左滑push操作,默认是NO */
    @property (nonatomic, assign) BOOL gk_openScrollLeftPush;
    
    

    UIViewController

    /** 是否禁止当前控制器的滑动返回(包括全屏返回和边缘返回) */
    @property (nonatomic, assign) BOOL gk_interactivePopDisabled;
    
    /** 是否禁止当前控制器的全屏滑动返回 */
    @property (nonatomic, assign) BOOL gk_fullScreenPopDisabled;
    
    /** 全屏滑动时,滑动区域距离屏幕左边的最大位置,默认是0:表示全屏都可滑动 */
    @property (nonatomic, assign) CGFloat gk_popMaxAllowedDistanceToLeftEdge;
    
    /** 设置导航栏的透明度 */
    @property (nonatomic, assign) CGFloat gk_navBarAlpha;
    
    /** push代理 */
    @property (nonatomic, assign) id<GKViewControllerPushDelegate> gk_pushDelegate;
    

    GKNavigationBarViewController

    /**
    自定义导航条
    */
    @property (nonatomic, strong, readonly) UINavigationBar *gk_navigationBar;
    
    /**
    自定义导航栏栏目
    */
    @property (nonatomic, strong, readonly) UINavigationItem *gk_navigationItem;
    
    #pragma mark - 额外的快速设置导航栏的属性
    @property (nonatomic, strong) UIColor *gk_navBarTintColor;
    @property (nonatomic, strong) UIColor *gk_navBackgroundColor;
    @property (nonatomic, strong) UIImage *gk_navBackgroundImage;
    @property (nonatomic, strong) UIColor *gk_navTintColor;
    @property (nonatomic, strong) UIView *gk_navTitleView;
    @property (nonatomic, strong) UIColor *gk_navTitleColor;
    @property (nonatomic, strong) UIFont *gk_navTitleFont;
    
    @property (nonatomic, strong) UIBarButtonItem *gk_navLeftBarButtonItem;
    @property (nonatomic, strong) NSArray<UIBarButtonItem *> *gk_navLeftBarButtonItems;
    
    @property (nonatomic, strong) UIBarButtonItem *gk_navRightBarButtonItem;
    @property (nonatomic, strong) NSArray<UIBarButtonItem *> *gk_navRightBarButtonItems;
    

    缺陷及不足

    • 不能使用系统导航栏的各种属性及方法

    github地址:
    GKNavigationController
    GKNavigationBarViewController

    简书地址:
    iOS自定义导航栏-导航栏联动(一)
    iOS自定义导航栏-导航栏联动(二)

    相关文章

      网友评论

      • Felix__He:大神,你好,问个问题,UINavigationController为什么用Category,而不去继承UINavigationController写一个啊?
        QuintGao:添加属性和方法用分类感觉更好点,无侵入性。继承也可以,但是会导致创建新的导航栏也需要继承,不然就无法使用新的属性和方法。

      本文标题:iOS自定义导航栏-导航栏联动(二)

      本文链接:https://www.haomeiwen.com/subject/bfdlkxtx.html