zoukankan      html  css  js  c++  java
  • iOS开发初探篇——UINavigationBar及UINavigationController

    概述


    导航在iOS开发中的重要性就不多说了。通常UINavigationController会结合UITabBar一起使用,此初探篇是单独对UINavigationBar及UINavigationController做一个比较全面的基础使用。

    本文主要内容如下:

      1.UINavigationBar的基础使用

          2.UINavigationController的基础使用

    UINavigationBar的基础使用


    UINavigationBar一般位于屏幕的顶端。继承自UIView。具体的结构图如下所示。

    每个NavigationItem都有N个BarButtonItem。

    先试一个最基本的代码:

    - (void)viewDidLoad {
        [super viewDidLoad];
        // Do any additional setup after loading the view, typically from a nib.
        
        
        _naviBar = [[UINavigationBar alloc]init];
        _naviBar.barTintColor = [UIColor blueColor];
        _naviBar.frame = CGRectMake(0, 20, self.view.frame.size.width, 44);
        
        [self.view addSubview:_naviBar];
        
        //naviItem
        UINavigationItem *naviItem1 = [[UINavigationItem alloc]initWithTitle:@"view1"];
        
         naviItem1.leftBarButtonItem = [[UIBarButtonItem alloc]initWithBarButtonSystemItem: UIBarButtonSystemItemAdd target:self action:@selector(push)];
       // naviItem1.leftBarButtonItem.title = @"aa";
        [_naviBar pushNavigationItem:naviItem1 animated:YES];
            
    }
    
    -(void) push {
        UINavigationItem *naviItem2 = [[UINavigationItem alloc]initWithTitle:@"view2"];
        [_naviBar pushNavigationItem:naviItem2 animated:YES];
    }
    

    注意导航栏背景颜色的设置使用barTintColortintColor是设置导航栏上按钮的颜色。官方文档的说明如下:

    /*
    
    The behavior of tintColor for bars has changed on iOS 7.0. It no longer affects the bar's background
    and behaves as described for the tintColor property added to UIView.
    To tint the bar's background, please use -barTintColor.
    */
    @property(nonatomic,retain) UIColor *tintColor;
    @property(nonatomic,retain) UIColor *barTintColor NS_AVAILABLE_IOS(7_0) UI_APPEARANCE_SELECTOR; // default is nil 

    UINavigationController的基础使用


     一般很少单独使用UINavigationBar,NaviContr会实用一些,里面已经封装了NaviBar。先看API中的UIView的类目声明,可以帮助理解。

    @interface UIViewController (UINavigationControllerItem)
    
    @property(nonatomic,readonly,retain) UINavigationItem *navigationItem; // Created on-demand so that a view controller may customize its navigation appearance.
    @property(nonatomic) BOOL hidesBottomBarWhenPushed; // If YES, then when this view controller is pushed into a controller hierarchy with a bottom bar (like a tab bar), the bottom bar will slide out. Default is NO.
    @property(nonatomic,readonly,retain) UINavigationController *navigationController; // If this view controller has been pushed onto a navigation controller, return it.
    
    @end
    

    (1)首先,有个很容易犯错的地方。每个视图是一个VC,而VC里已经自带了NaviItem,无需再去创建,如果用类似 self.navigationItem = [[UINavigationItem alloc]init]去创建会报错。因为是readOnly。我们只需调用naviItem里的属性去设置即可。

    //1.
        self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc]initWithTitle:@"next" style:UIBarButtonItemStylePlain target:self action:nil];
        
    //2.
        self.navigationController.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc]initWithTitle:@"next" style:UIBarButtonItemStylePlain target:self action:nil];
    

    具体一点儿来说就是每一个加到navigationController的viewController都会有一个对应的navigationItem,该对象由viewController以懒加载的方式创建,稍后我们可以在对象中对 navigationItem进行配置,可以设置leftBarButtonItem, rightBarButtonItem, backBarButtonItem, title以及prompt等属性。个人理解:NaviController在这里只有一个,但每个导航进去的页面都有一个naviItem的。

    (2)其次,用1.创建会有效果。2.创建没效果。至于为什么没效果,个人理解:在这种情况下,NaviControler全部只有一个,但NaviItem是每个导航进去的页面就对应一个的,那么 self.navigationController.navigationItem到底是哪个?系统识别不出来啊!

    这里很容易犯错的一个地方,就是关于backBarButtonItem,并不是在本页中设置成的返回按钮。API中说明如下:Bar button item to use for the back button in the child navigation item.这个按钮是显示在子页中的!!!!!!

    @property(nonatomic,retain) UIBarButtonItem *backBarButtonItem; // Bar button item to use for the back button in the child navigation item.
    

    其实也很好理解,它的主角是本页,那么能够返回到本页的按钮需要在本页中进行设置,在子页中进行显示。 

    (3)先看下面代码:

        self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc]initWithTitle:@"next" style:UIBarButtonItemStylePlain target:self action:@selector(push)];
        self.navigationItem.title = @"first";
        
        self.navigationController.navigationBar.barTintColor = [UIColor redColor];

      最后一句关于导航栏颜色的设置,其实NaviCntroller就只有一个,naviBar也是只有一个,那么在其中一个VC中写了这么一句配置颜色的代码,是会改变每一个VC中导航栏的背景颜色的,因为本来就公用的一个NaviBar嘛,只是有N个NaviItems而已。那么有什么办法使得每个VC中的导航栏背景颜色不一样呢?easy,只需要在VC的willappear中重新配置颜色即可。

     (4)关于每个页面中返回键:可以通过两种方式进行设置。

        办法1:在上个父界面中的backButton中设置,这个在之前中介绍过了,代码如下:

        UIBarButtonItem *but = [[UIBarButtonItem alloc]init];
        [but setTitle:@"back"];
        
        self.navigationItem.backBarButtonItem = but;
    

        办法2:在本视图界面中设置leftButton,这样的话要绑定动作事件,同时返回的图标是不存在的。代码如下:

    - (void)viewDidLoad {
        [super viewDidLoad];
        // Do any additional setup after loading the view.
        
        self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc]initWithTitle:nil style:UIBarButtonItemStylePlain target:self action:nil];
        self.navigationItem.title = @"second";
        
        self.navigationController.navigationBar.barTintColor = [UIColor yellowColor];
    
        self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc]initWithTitle:@"back" style:UIBarButtonItemStylePlain target:self action:@selector(pop)];
    }
    
    -(void)pop {
        [self.navigationController popViewControllerAnimated:YES];
    }
  • 相关阅读:
    Java是如何实现平台无关性的
    Java 语法糖详解
    深入分析Java的编译原理
    Java代码的编译与反编译那些事儿
    Java 源码学习系列(三)——Integer
    总结TESTNG与JUNIT的异同
    selenium如何操作cookies实现免登录
    selenium选择弹出窗口
    Selenium+Java(七)Selenium对话框的处理
    Java-控制台接受用户输入数据的方法
  • 原文地址:https://www.cnblogs.com/hushunfeng/p/4605550.html
Copyright © 2011-2022 走看看