美文网首页
UINavigationController底层实现原理解析

UINavigationController底层实现原理解析

作者: 心亦逸风 | 来源:发表于2019-02-13 15:15 被阅读1次

在日常开发中,UINavigationController是我们常用的一种视图控制器,也许平时我们也有碰到一些问题并且查找了解决方法,但是并不清楚它的具体实现的方式和原理,相信了解后,会对之后的开发很有帮助。
接来我会按点分析:

1.UINavigationControllrt结构

创建一个UINavigationController并设置一个rootViewController,查看它的层级:


UINavigationController层级

可以看到它整个,整个内容在UILayoutContainerView上,在这之上有三部分组成转场视图UINacigationTransitionView、导航视图UINavigationBar和工具栏视图UIToolBar(在最底部默认是不在,通过toolbarHidden属性添加上去)。
而在转场视图中的UIViewControllerWrapperView则是真正控制显示的ViewController。

2.导航区navigationBar对于内容区域view的影响

@property(nonatomic,assign,getter=isTranslucent) BOOL translucent NS_AVAILABLE_IOS(3_0) UI_APPEARANCE_SELECTOR;
 // Default is NO on iOS 6 and earlier. Always YES if barStyle is set to UIBarStyleBlackTranslucent

在navigationBar上的这个属性是控制导航区是否半透明。
在iOS7之前,苹果是拟物化风格,导航区默认是不透明的,内容区的view都是从导航区下方开始的(64),而iOS7之后,转为扁平化,默认为半透明。
当我们把该属性为NO,也就是不透明时,它的表现和之前一样,所有放在内容区ViewController上的View都是从导航区下方开始为起点(即64,在iPhoneX等全面屏机型上为84,为方便之后文中都用64代表状态栏+导航栏的高度)。
而当为默认半透明时,放在ViewController上的View起点都是0。
这里有一点需要注意,当view是放在UIScrollView或其子类上时,这个view会和导航区没有设置透明的表现一样,在导航区下方开始。经过研究,打印scrollView的bounds会发现它的y值为-64。在UIViewController上有一个属性

@property(nonatomic,assign) BOOL automaticallyAdjustsScrollViewInsets API_DEPRECATED("Use UIScrollView's contentInsetAdjustmentBehavior instead", ios(7.0,11.0),tvos(7.0,11.0)); 
// Defaults to YES

字面意思理解就是自动调节scrollViewInset,默认YES,目的是不让导航栏遮住在scrollView上的内容,在iOS11被弃用,用UIScrollView上的contentInsetAdjustmentBehavior属性代理。

3.关于popViewControllerAnimated方法

在navigationController上有一个viewControllers的数组,里面装着在NationController中所有push进来的ViewController,我们依次把5个vc给push进来,查看navigationController. viewControllers会发现这5个vc依次在数组中,我们把这5个vc依次叫1-5vc,这时候我们移除数组中最上面两个vc,并调用popViewControllerAnimated方法:

    NSMutableArray *array = [NSMutableArray arrayWithArray:self.navigationController.viewControllers];
    [array removeLastObject];
    [array removeLastObject];
    self.navigationController.viewControllers = array;

    [self.navigationController popViewControllerAnimated:YES];

我们会发现这时候会返回到3vc,由此,我们可以知道popViewControllerAnimated的逻辑是移除当前显示控制器,若没有则不移除viewControllers数组中的vc跳到栈顶控制器。

4.关于navigationBar

(1)navigationBar只有一个,通过当前控制器的navigationItem显示内容,如果有需要而直接在navigationBar上添加view,必须在不需要的时候进行移除(如退出当前vc或进入下一vc时),否则会一直存在。
(2)关于设置导航区的透明,首先需要把半透明关闭
self.navigationController.navigationBar.translucent = NO;
然后有两种方式,一种是设置图片方法:

- (void)transluentStyle{
    [self.navigationController.navigationBar setBackgroundImage:self.image forBarMetrics:UIBarMetricsDefault];
}

- (UIImage*)image{
    UIGraphicsBeginImageContext(CGSizeMake(100, 100));
    [[[UIColor whiteColor] colorWithAlphaComponent:0] setFill];
    UIRectFill(CGRectMake(0, 0, 100, 100));
    UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();    
    return image;
}

一种是找到背景显示的view并把它设置为透明颜色:

- (void)transluentTwoStyle{
    NSArray *ary  = [self.navigationController.navigationBar subviews];
    UIColor *alphaColor = [[UIColor whiteColor] colorWithAlphaComponent:0];
    for (int i = 0; i < ary.count; i++) {
        UIView *view = ary[i];
        view.backgroundColor = alphaColor;

        for (int j = 0; j < view.subviews.count; j++) {
            UIView *subView = view.subviews[j];
            subView.backgroundColor = alphaColor;
            for (int k = 0; k < subView.subviews.count; k++) {
                UIView *subsubView = subView.subviews[k];
                subsubView.backgroundColor = alphaColor;
            }
        }
        if([view isKindOfClass:NSClassFromString(@"_UIBarBackground")]){
            view.backgroundColor = alphaColor;
        }
    } 
}

(3)导航区下方的黑线去除,因为在层级图上找到黑线的view,发现它的y值是64,说明高于导航区而又在导航区上,这时候只要设置下导航区超过不显示即可:
self.navigationController.navigationBar.clipsToBounds = YES;
(4)当需要在navigationBar上添加多个按钮并需要有一点间隔时可以这样做:

    NSMutableArray *barItems = [NSMutableArray array];
    
    UIBarButtonItem *barItem = [[UIBarButtonItem alloc] initWithTitle:@"Nav" style:UIBarButtonItemStylePlain target:self action:@selector(showNav)];
    UIBarButtonItem *barItemSpace = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFixedSpace target:nil action:nil];
    barItemSpace.width = 60;
    UIBarButtonItem *barItemT = [[UIBarButtonItem alloc] initWithTitle:@"view" style:UIBarButtonItemStylePlain target:self action:@selector(showNavTwo)];
    
    [barItems addObject:barItem];
    [barItems addObject:barItemSpace];
    [barItems addObject:barItemT];
    
    self.navigationItem.rightBarButtonItems = barItems;

(5)关于navigationbarHidden方法,它是通过remove的方法将navigationBar隐藏的,所以如果要显示,不能用navigationbar.hidden = NO;的方式。

如果觉得有用,请随手给一个赞,谢谢:)

相关文章

网友评论

      本文标题:UINavigationController底层实现原理解析

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