• App开发流程之状态栏和导航栏


    记录状态栏和导航栏的设置和控制,统一在基类视图控制器中完成。

    状态栏。

    状态栏高度为20,iOS7以后背景完全透明。

    样式枚举如下:

    typedef NS_ENUM(NSInteger, UIStatusBarStyle) {

        UIStatusBarStyleDefault                                     = 0, // Dark content, for use on light backgrounds

        UIStatusBarStyleLightContent     NS_ENUM_AVAILABLE_IOS(7_0) = 1, // Light content, for use on dark backgrounds    

        UIStatusBarStyleBlackTranslucent NS_ENUM_DEPRECATED_IOS(2_0, 7_0, "Use UIStatusBarStyleLightContent") = 1,

        UIStatusBarStyleBlackOpaque      NS_ENUM_DEPRECATED_IOS(2_0, 7_0, "Use UIStatusBarStyleLightContent") = 2,

    } ;

    动画枚举如下:

    typedef NS_ENUM(NSInteger, UIStatusBarAnimation) {

        UIStatusBarAnimationNone,

        UIStatusBarAnimationFade,

        UIStatusBarAnimationSlide,

    } ;

     

    旧的常用设置方式:

    [[UIApplication sharedApplication] setStatusBarHidden:YES withAnimation:UIStatusBarAnimationFade];

    [[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleDefault];

    但是iOS9以后废弃了这些方法。就算是在iOS7上,如果启用了基于UIViewController的状态栏控制系统,上述方法也将失效。

    UIApplication.h中的注释内容为:Setting the statusBarStyle/statusBarHidden does nothing if your application is using the default UIViewController-based status bar system.

    iOS7以后默认开启了UIViewController的状态栏控制系统。如果需要关闭,并采用旧的设置方式,可以将Info.plist中键值View controller-based status bar appearance设置为NO。

     

    新的设置方式:

    - (UIStatusBarStyle)preferredStatusBarStyle; // Defaults to UIStatusBarStyleDefault

    - (BOOL)prefersStatusBarHidden; // Defaults to NO

    - (UIStatusBarAnimation)preferredStatusBarUpdateAnimation; // Defaults to UIStatusBarAnimationFade

    // This should be called whenever the return values for the view controller's status bar attributes have changed. If it is called from within an animation block, the changes will be animated along with the rest of the animation block.

    - (void)setNeedsStatusBarAppearanceUpdate;

    上述方法,均为UIViewController的实例方法,前三个方法可以在子类中被重写,最后主动调用setNeedsStatusBarAppearanceUpdate才会生效。

     

    为了便于切换控制方式,提供宏控制方案:

    1.在Info.plist文件中增加键值View controller-based status bar appearance = NO

    2.在基类控制器的头文件中定义如下宏,便于子类检查是否启用该宏定义

    //使用基于视图控制器的状态栏控制系统,UIApplicationsetStatusBarHidden类似方法将无效

    //开启此宏定义,请将Info.plistView controller-based status bar appearance设置为YES,否则置为NO

    #define UseDefaultUIViewControllerBasedStatusBarSystem

    3.提供相关属性以设置状态栏,提供相关方法更新状态栏,并在实现文件中增加条件判断宏

    #ifdef UseDefaultUIViewControllerBasedStatusBarSystem
    
    - (UIStatusBarStyle)preferredStatusBarStyle
    {
        return self.statusBarStyle;
    }
    
    - (BOOL)prefersStatusBarHidden
    {
        return self.willHideStatusBar;
    }
    
    - (UIStatusBarAnimation)preferredStatusBarUpdateAnimation
    {
        return UIStatusBarAnimationFade;
    }
    
    #endif
    
    - (void)updateStatusBar
    {
    #ifdef UseDefaultUIViewControllerBasedStatusBarSystem
        [self setNeedsStatusBarAppearanceUpdate];
    #else
        [[UIApplication sharedApplication] setStatusBarHidden:self.willHideStatusBar withAnimation:UIStatusBarAnimationFade];
        [[UIApplication sharedApplication] setStatusBarStyle:self.statusBarStyle];
    #endif
    }

    导航栏

    导航栏高44,iOS7以后默认透明,由UINavigationBar的translucent属性决定

    1.设置导航栏显示属性,更多是在设置UINavigationController的UINavigationBar属性。UIViewController中也存在UINavigationController类型的属性,参考如下:

    [self.navigationController.navigationBar setBarStyle:UIBarStyleDefault];
    [self.navigationController.navigationBar setTranslucent:YES];
    [self.navigationController.navigationBar setBackgroundImage:[ImageHelper getImageWithColor:NaviBarColor] forBarMetrics:UIBarMetricsDefault];
    [self.navigationController.navigationBar setShadowImage:[ImageHelper getImageWithColor:NaviBarShadowColor]];
    [self.navigationController.navigationBar setTitleTextAttributes:NaviBarTitleAttributes];

    2.设置标题、按钮相关属性,关注UIViewController的UINavigationItem属性

    3.隐藏导航栏,可以设置navigationBarHidden属性,但建议使用方法

    - (void)setNavigationBarHidden:(BOOL)hidden animated:(BOOL)animated; // Hide or show the navigation bar. If animated, it will transition vertically using UINavigationControllerHideShowBarDuration.

    如果上一个界面显示导航栏,下一个界面隐藏导航栏(带有返回上一个界面的功能方法),界面切换可能出现导航栏过渡效果不佳的情况。在viewWillAppear方法中,使用代码

    [self.navigationController setNavigationBarHidden:self.willHideNavigationBar animated:animated];

    可以得到较好的切换效果。

    4.如果带有bottomBar,当新的UIViewController将要被push到导航栈top前,可以使用UIViewController的hidesBottomBarWhenPushed控制bottomBar显示

    5.合理使用导航栏控制的属性viewControllers和方法:

    - (void)setViewControllers:(NSArray<UIViewController *> *)viewControllers animated:(BOOL)animated NS_AVAILABLE_IOS(3_0); // If animated is YES, then simulate a push or pop depending on whether the new top view controller was previously in the stack.

    可以实现导航栈中的视图控制器切换、替换、新增、移除等功能效果。

    6.导航控制的如下代理方法,可以实现切换动画的自定义,后续记录一个实现返回手势控制的分类将涉及到

    - (nullable id <UIViewControllerInteractiveTransitioning>)navigationController:(UINavigationController *)navigationController

                              interactionControllerForAnimationController:(id <UIViewControllerAnimatedTransitioning>) animationController NS_AVAILABLE_IOS(7_0); 

    - (nullable id <UIViewControllerAnimatedTransitioning>)navigationController:(UINavigationController *)navigationController

                                       animationControllerForOperation:(UINavigationControllerOperation)operation

                                                    fromViewController:(UIViewController *)fromVC

                                                      toViewController:(UIViewController *)toVC  NS_AVAILABLE_IOS(7_0);

     

    UIViewController的self.view布局问题

    单独一个视图控制器的self.view应该是充满容器的,origin为(0, 0),增加导航栏以后,如果不设置,仍然为(0, 0),将增加subview布局的麻烦。所以,需要关注如下属性:

    @property(nonatomic,assign) UIRectEdge edgesForExtendedLayout NS_AVAILABLE_IOS(7_0); // Defaults to UIRectEdgeAll

    @property(nonatomic,assign) BOOL extendedLayoutIncludesOpaqueBars NS_AVAILABLE_IOS(7_0); // Defaults to NO, but bars are translucent by default on 7_0.  

    extendedLayoutIncludesOpaqueBars属性,该属性表示是否包括不透明bars,默认不包括,但是iOS7以后,状态栏和导航栏都是默认透明的,所以self.view默认与屏幕边缘贴合。

    edgesForExtendedLayout决定了self.view与边缘的距离,默认贴合边框,当增加导航栏以后,需要设置为UIRectEdgeNone,使self.view上边缘与导航栏下边缘贴合。

     

    下图为self.edgesForExtendedLayout = UIRectEdgeNone;

    说明:导航栏下的红条origin为(0, 0)且高20,黑条高44,蓝条高44。

     

    下图为self.edgesForExtendedLayout = UIRectEdgeAll;

     

    无导航栏时候,两种设置,红条都靠着顶部。

    UIViewController的automaticallyAdjustsScrollViewInsets属性。

    该属性自动设置self.view上scrollView类型的subview的insets,默认为YES。

    当导航栏隐藏时候,该属性会导致scrollView的contentView的offsetY自动为20,也就是看到scrollView内容从状态栏下边缘开始显示,这也将带来很多麻烦。

    所以导航栏隐藏时候设置self.automaticallyAdjustsScrollViewInsets = !willHideNavigationBar;可以避免意外的显示异常。 

     

    下一篇记录一个实现返回手势的分类。

     

    base项目已更新:git@github.com:ALongWay/base.git

  • 相关阅读:
    Java实现蓝桥杯 算法训练 Professor Monotonic's Network
    Java实现 蓝桥杯 算法训练 数字游戏
    Java实现 蓝桥杯 算法训练 数字游戏
    Java实现 蓝桥杯 算法训练 数字游戏
    Java实现 LeetCode 264 丑数 II(二)
    Java实现 LeetCode 264 丑数 II(二)
    linux下c/c++方式访问curl的帮助手册
    boost 1.56.0 编译及使用
    使用Boost.Asio编写通信程序
    webrtc教程
  • 原文地址:https://www.cnblogs.com/ALongWay/p/5891952.html
Copyright © 2020-2023  润新知