【IOS开发初学者】UINavigationController详解

在这里我要说一下,ios开发的知识点都偏于基础,我只记录了一些基础的知识点,并进行了扩展,适合入门的朋友:
【IOS初学者】UITableView与自定义UITableViewCell
【IOS初学者】bundle知识点总结
【IOS初学者】数组与字典javascript


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

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

不使用navigationController

不使用navigationController的跳转界面

使用navigationController

使用navigationController的跳转界面

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

UINavigationController view层级

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

Paste_Image.png

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

UINavigationItem

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

// 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中的一个组件,咱们能够直接设置,例如:ui

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

运行结果以下:atom

Paste_Image.png

UINavigationItem继承自NSObject,包含了当前页面导航栏上须要显示的所有信息:title,prompt,titleView,leftBarButtonItem,rightBarButtonItem,backBarButonItemspa

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。
相关文章
相关标签/搜索