0129——UINavigationController

1.创建一个UINavigationController

    self.window = [[UIWindow alloc]initWithFrame:[UIScreen mainScreen].bounds];   

    FirstViewController * first = [[FirstViewController alloc]init];    

    UINavigationController * nav = [[UINavigationController alloc]initWithRootViewController:first];  UINavigationController第一个显示的界面

    self.window.rootViewController = nav;

    [self.window makeKeyAndVisible];

2.隐藏导航栏工具栏

    self.navigationController.navigationBarHidden = YES;

    self.navigationController.toolbarHidden = NO;

3.设置导航栏和工具栏的颜色(设置了之后所有都是这个颜色,因为只有一个navigationController)

    self.navigationController.toolbar.barTintColor = [UIColor redColor];

    self.navigationController.navigationBar.barTintColor = [UIColor redColor];

4.设置左右显示的按钮

    UIBarButtonItem * leftBtn = [[UIBarButtonItem alloc]initWithTitle:@"Cancel" style:UIBarButtonItemStyleDone target:nil action:nil];

    self.navigationItem.leftBarButtonItem = leftBtn;

    self.navigationItem.leftBarButtonItem.tintColor = [UIColor whiteColor];

    

    UIBarButtonItem * rightBtn = [[UIBarButtonItem alloc]initWithTitle:@"Next" style:UIBarButtonItemStyleDone target:self action:@selector(gotoSecondView)];

    self.navigationItem.rightBarButtonItem = rightBtn;

    self.navigationItem.rightBarButtonItem.tintColor = [UIColor whiteColor];

5.设置标题

    self.navigationItem.title =@"标题";

6.标题位置设置成view

    UIView * view = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 100, 44)];

    view.backgroundColor = [UIColor yellowColor];

    self.navigationItem.titleView = view;

7.Next按钮响应第二个界面

-(void)gotoSecondView{

    SecondViewController * svc = [[SecondViewController alloc]init];

    //设置默认提供的返回按钮的标题

    UIBarButtonItem *backItem = [[UIBarButtonItem allocinitWithTitle:@"返回" style:UIBarButtonItemStyleDone target:nil action:nil];

    self.navigationItem.backBarButtonItem = backItem;

    //present:在现有的界面上盖上一层 dismissViewController来消除

    //[self.navigationController presentViewController:svc animated:YES completion:nil];

    //push:push入栈 pop出栈来消除

    [self.navigationController pushViewController:svc animated:YES];

}

原文地址:https://www.cnblogs.com/damonWq/p/5168993.html