美文网首页Android IOS移动开发程序员iOS Developer
[IOS开发初学者]UINavigationController

[IOS开发初学者]UINavigationController

作者: mymdeep | 来源:发表于2017-02-03 11:05 被阅读4927次

    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];
    

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

    不使用navigationController 不使用navigationController的跳转界面 使用navigationController 使用navigationController的跳转界面

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

    UINavigationController view层级

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

    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";
    

    运行结果如下:

    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。

    相关文章

      网友评论

        本文标题:[IOS开发初学者]UINavigationController

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