博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
iOS边练边学--UINavigationController导航条的使用
阅读量:7051 次
发布时间:2019-06-28

本文共 2815 字,大约阅读时间需要 9 分钟。

一、使用UINavigationController的步骤以及代码

1 // 程序加载完成后执行的代码 2 - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { 3     // 1.初始化窗体 4     self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]]; 5      6     // 2.导航控制器的根控制器 7     UIViewController *vc = [[ViewController alloc] init]; 8     vc.view.backgroundColor = [UIColor lightGrayColor]; 9     // 3.窗体的根控制器为导航控制器10     UINavigationController *nav = [[UINavigationController alloc] initWithRootViewController:vc];11     12     // 4.设置窗体根控制器13     self.window.rootViewController = nav;14     15     // 5.显示窗体16     [self.window makeKeyAndVisible];17     return YES;18 }
二、UINavigationController的子控制器
  <1>UINavigationController以栈的形式保存子控制器(先进后出,看成数组就成)

  @property(nonatomic,copy) NSArray *viewControllers;

  @property(nonatomic,readonly) NSArray *childViewControllers;

  <2>使用push方法能将某个控制器压入栈

  - (void)pushViewController:(UIViewController *)viewController animated:(BOOL)animated;

  <3>使用pop方法可以移除控制器

  将栈顶的控制器移除

  - (UIViewController *)popViewControllerAnimated:(BOOL)animated;

  <4>回到指定的子控制器

  - (NSArray *)popToViewController:(UIViewController *)viewController animated:(BOOL)animated;

  <5>回到根控制器(栈底控制器)

  - (NSArray *)popToRootViewControllerAnimated:(BOOL)animated;

三、如何修改导航栏的内容

  
<注意!!!!>导航栏的内容由栈顶控制器的navigationItem属性决定
  UINavigationItem有以下属性影响着导航栏的内容
  <1>左上角的返回按钮

  @property(nonatomic,retain) UIBarButtonItem *backBarButtonItem;

  <2>中间的标题视图

  @property(nonatomic,retain) UIView *titleView;

  <3>中间的标题文字

  @property(nonatomic,copy)NSString *title;

  <4>左上角的视图

  @property(nonatomic,retain) UIBarButtonItem *leftBarButtonItem;

  <5>UIBarButtonItem *rightBarButtonItem  右上角的视图

  @property(nonatomic,retain) UIBarButtonItem *rightBarButtonItem;

  <6>代码实现:

1 - (void)viewDidLoad { 2     [super viewDidLoad]; 3     // 这段代码是错误的!--导航栏的内容由栈顶控制器的navigationItem属性决定,并不是有导航栏本身决定! 4 //    self.navigationController.navigationItem.title = @"第一个导航"; 5     self.navigationItem.title = @"第一个导航"; 6     // 下面这一句跟上面的代码是同一个意思,简化写法 7 //    self.title = @"这个管那里????"; 8     // 可以设置UIView 9     self.navigationItem.titleView = [UIButton buttonWithType:UIButtonTypeContactAdd];10     // 左右两边的按钮系统默认渲染成蓝色的,并且位置改动不了11     self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"没用" style:UIBarButtonItemStyleDone target:nil action:nil];12     // 右边按钮不受系统渲染的方法一:initWithCustomView13     UIButton *btn = [[UIButton alloc] init];14     [btn setImage:[UIImage imageNamed:@"navigationbar_friendsearch"] forState:UIControlStateNormal];15     [btn setImage:[UIImage imageNamed:@"navigationbar_friendsearch_highlighted"] forState:UIControlStateHighlighted];16     [btn sizeToFit];17     18     self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:btn];19 }

转载地址:http://tkpol.baihongyu.com/

你可能感兴趣的文章
《音乐达人秀:Adobe Audition CC实战222例》——1.2 从双卡录音机到多轨录音软件...
查看>>
年度回顾 看看 2016 年编程语言发展趋势
查看>>
vue axios+springboot 文件下载
查看>>
JS的继承方式
查看>>
掘金广告产品介绍
查看>>
九宫格
查看>>
手把手教你写一个 VSCode 插件
查看>>
cookie和session
查看>>
使用 multipart/x-mixed-replace 实现 http 实时视频流
查看>>
史上最牛内推小组(持续更新)
查看>>
现实中的路由规则,可能比你想象中复杂的多
查看>>
nginx配置gzip中的坑
查看>>
Javascript中的函数声明与函数表达式
查看>>
Python学习笔记 - queue
查看>>
茶器漫谈 高逼格 or 真内涵?
查看>>
HTML5学习之Web Storage基础知识
查看>>
tab切换
查看>>
垃圾回收及内存调试工具的介绍
查看>>
你的接口,真的能承受高并发吗?
查看>>
自定义View实用小技巧
查看>>