UINavigationController的简单介绍

UINavigationController是IOS编程中比较常用的一种容器view controller,也就是导航栏控制器。

    
(这里是文档里的图片)

1.创建一个UINavigationController

    self.window = [[UIWindow alloc]initWithFrame:[UIScreen mainScreen].bounds];   
    FirstViewController * first = [[FirstViewController alloc]init];    
    UINavigationController * nav = [[UINavigationControlleralloc]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:UIBarButtonItemStyleDonetarget:nil action:nil];
    self.navigationItem.leftBarButtonItem = leftBtn;
    self.navigationItem.leftBarButtonItem.tintColor = [UIColor whiteColor];
    
    UIBarButtonItem * rightBtn = [[UIBarButtonItem alloc]initWithTitle:@"Next" style:UIBarButtonItemStyleDonetarget:self action:@selector(gotoSecondView)];
    self.navigationItem.rightBarButtonItem = rightBtn;
    self.navigationItem.rightBarButtonItem.tintColor = [UIColor whiteColor];

5.设置标题

    self.navigationItem.title =@"标题";
    self.navigationBar.titleTextAttributes = @{NSFontAttributeName:[UIFontsystemFontOfSize:16],NSForegroundColorAttributeName:[UIColor whiteColor]};

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 alloc] initWithTitle:@"返回" 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];
}
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 发现 关注 消息 iOS 第三方库、插件、知名博客总结 作者大灰狼的小绵羊哥哥关注 2017.06.26 09:4...
    肇东周阅读 14,369评论 4 61
  • Swift版本点击这里欢迎加入QQ群交流: 594119878最新更新日期:18-09-17 About A cu...
    ylgwhyh阅读 25,698评论 7 249
  • 没有期待的开始, 和不曾预测的结束。 一切就如天空中的云朵, 仿佛全部是自然而然, 又却像早已注定。 驻足,凝望,...
    芯满亦足阅读 2,578评论 0 4
  • 有时候,一时间就突然明白了什么!可停下来,真正的追问自己明白了什么?又说不出来,稀里糊涂,不知自己所云。只是有一种...
    远山不远阅读 3,030评论 0 0
  • 我们如同一个个木偶, 生活在别人的操纵下! 我们如同一个个螺丝钉, 在社会这个机器上不断运行!
    石成朝阅读 1,040评论 2 2

友情链接更多精彩内容