[IOS开发初学者]UINavigationController详解

UINavigationController是IOS编程中比较常用的一种viewcontroller,在介绍它的功能之前,我们先对比一下是否使用UINavigationController,在界面上有什么异同:

  UINavigationController *navigationController = [[UINavigationController alloc]initWithRootViewController:[MainViewController new]];
    self.window = [[UIWindow alloc] initWithFrame:[UIScreen mainScreen].bounds];
    [self.window makeKeyAndVisible];
  //使用navigationController
   // self.window.rootViewController = navigationController;
//不使用navigationController
    self.window.rootViewController = [EmptyViewController new];

根据以上代码,我们可以看到如下的运行结果:

《[IOS开发初学者]UINavigationController详解》 不使用navigationController
《[IOS开发初学者]UINavigationController详解》 不使用navigationController的跳转界面
《[IOS开发初学者]UINavigationController详解》 使用navigationController
《[IOS开发初学者]UINavigationController详解》 使用navigationController的跳转界面

根据上图对比可知,使用navigationController比普通的viewcontroller多了上面一层导航条,可以更方便的控制界面的跳转。

UINavigationController view层级

介绍UINavigationController view层级,我们先从一张图开始:

《[IOS开发初学者]UINavigationController详解》 Paste_Image.png

UINavigationController也是一个ViewController,并不仅仅是一个导航条,它是在普通的ViewController基础上增加了新的功能。根据上面的图,我们进行逐步的分析。

UINavigationItem

我们都知道navigationItem是UIViewController的一个属性,这个属性是为UINavigationController服务的。
查看源码,UINavigationItem中有两个比较常用的item:

// Some navigation items want to display a custom left or right item when they're on top of the stack.
// A custom left item replaces the regular back button unless you set leftItemsSupplementBackButton to YES
@property(nullable, nonatomic,strong) UIBarButtonItem *leftBarButtonItem;
@property(nullable, nonatomic,strong) UIBarButtonItem *rightBarButtonItem;

也就是导航栏上的左右button,他们都是navigationItem中的一个组件,我们可以直接设置,例如:

    UIBarButtonItem *rightItem = [[UIBarButtonItem alloc] initWithTitle:@"Root"
                                                                  style:UIBarButtonSystemItemAction
                                                                 target:self
                                                                 action:@selector(butClick)];
    self.navigationItem.rightBarButtonItem = rightItem;
    self.navigationItem.title = @"Hello, im the title";

运行结果如下:

《[IOS开发初学者]UINavigationController详解》 Paste_Image.png

UINavigationItem继承自NSObject,包含了当前页面导航栏上需要显示的全部信息:title,prompt,titleView,leftBarButtonItem,rightBarButtonItem,backBarButonItem

UINavigationBar

UINavigationBar继承自UIView,看一下他的属性可知,他主要是对UINavigationItem进行管理的。

@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

// Pushing a navigation item displays the item's title in the center of the navigation bar.
// The previous top navigation item (if it exists) is displayed as a "back" button on the left.
- (void)pushNavigationItem:(UINavigationItem *)item animated:(BOOL)animated;
- (nullable UINavigationItem *)popNavigationItemAnimated:(BOOL)animated; // Returns the item that was popped.

@property(nullable, nonatomic,readonly,strong) UINavigationItem *topItem;
@property(nullable, nonatomic,readonly,strong) UINavigationItem *backItem;

@property(nullable,nonatomic,copy) NSArray<UINavigationItem *> *items;
- (void)setItems:(nullable NSArray<UINavigationItem *> *)items animated:(BOOL)animated; 
........

UINavigationBar与UINavigationItem的区别

  • UINavigationBar是继承自UIView的,UINavigationItem继承自NSObject的。各自的属性和功能不同
  • UINavigationBar并包含所有UINavigationItem的栈,管理整个NavigationController的UINavigationItem。
    原文作者:mymdeep
    原文地址: https://www.jianshu.com/p/ef1cd11a4b53
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞