UINavigationController通过栈的方式来管理视图,通过push将视图压入栈,pop将视图推出栈。

下面通过简单的示例说明

AppDelegate.m

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {

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

    ViewController *vc = [[ViewController alloc] init];

    UINavigationController *vcNav = [[UINavigationController alloc] initWithRootViewController:vc];
self.window.rootViewController = vcNav; [self.window makeKeyAndVisible]; return YES;
}

ViewController.m

- (void)viewDidLoad
{
[super viewDidLoad]; self.view.backgroundColor = [UIColor whiteColor];
/* 导航标题的设置可通过三种方式,且显示优先级如下:
self.navigationItem.titleView > self.navigationItem.title > self.title 例如这里我们同时使用前两种方式,则默认显示titleView的内容 */
//导航标题
UILabel *labelView = [[UILabel alloc] initWithFrame:CGRectMake(, , , )];
labelView.text = @"我是根视图";
labelView.textAlignment = NSTextAlignmentCenter;
self.navigationItem.titleView = labelView;
self.navigationItem.title = @"我不是根视图"; //左边导航按钮
UIButton *leftButton = [[UIButton alloc] initWithFrame:CGRectMake(, , , )];
[leftButton setTitle:@"按钮" forState:UIControlStateNormal];
[leftButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
UIBarButtonItem *leftItem = [[UIBarButtonItem alloc] initWithCustomView:leftButton];
self.navigationItem.leftBarButtonItem = leftItem;
//右边导航按钮
UIButton *rightButton = [[UIButton alloc] initWithFrame:CGRectMake(, , , )];
[rightButton setTitle:@"跳转" forState:UIControlStateNormal];
[rightButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
[rightButton addTarget:self action:@selector(clickedRightButton) forControlEvents:UIControlEventTouchUpInside];
UIBarButtonItem *rightItem = [[UIBarButtonItem alloc] initWithCustomView:rightButton];
self.navigationItem.rightBarButtonItem = rightItem; /* backBarButtonItem的使用
如果AController push到 BController,那么有以下几种情况
1:B设置了leftBarButtonItem,则优先显示B的leftBarButtonItem
2:B没有设置leftBarButtonItem,A设置了backBarButtonItem,则显示A设置的backBarButtonItem
3:B没有设置leftBarButtonItem,A没有设置backBarButtonItem,则系统会拿到A的title显示一个返回按钮
所以B左边按钮的显示优先级如下:
B的leftBarButtonItem > A的backBarButtonItem > 系统默认的返回按钮
大家可以先注释掉标号①的那行代码,然后同时注释掉标号①②的代码,来试验一下。 这里注意,backBarButtonItem只能自定义title和image,如下注释掉的方法是无效的。 */
//返回按钮
// UIButton *backButton = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 50, 30)];
// [backButton setTitle:@"返回" forState:UIControlStateNormal];
// [backButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
// UIBarButtonItem *backItem = [[UIBarButtonItem alloc] initWithCustomView:backButton];
UIBarButtonItem *backItem = [[UIBarButtonItem alloc] initWithTitle:@"我是返回" style:UIBarButtonItemStylePlain target:self action:nil];
self.navigationItem.backBarButtonItem = backItem; //② UILabel *tipsLabel = [[UILabel alloc] initWithFrame:CGRectMake(, , self.view.frame.size.width-, )];
tipsLabel.text = @"我是根视图";
tipsLabel.textAlignment = NSTextAlignmentCenter;
tipsLabel.textColor = [UIColor redColor];
[self.view addSubview:tipsLabel]; }
- (void)clickedRightButton
{
ChildViewController *childVC = [[ChildViewController alloc] init];
[self.navigationController pushViewController:childVC animated:YES];
}

ChildViewController.m

- (void)viewDidLoad
{
[super viewDidLoad]; self.view.backgroundColor = [UIColor lightGrayColor];
//导航标题
UILabel *labelView = [[UILabel alloc] initWithFrame:CGRectMake(, , , )];
labelView.text = @"我是子视图";
labelView.textAlignment = NSTextAlignmentCenter;
self.navigationItem.titleView = labelView;
//左边导航按钮
UIButton *leftButton = [[UIButton alloc] initWithFrame:CGRectMake(, , , )];
[leftButton setTitle:@"返回" forState:UIControlStateNormal];
[leftButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
[leftButton addTarget:self action:@selector(clickedLeftButton) forControlEvents:UIControlEventTouchUpInside];
UIBarButtonItem *leftItem = [[UIBarButtonItem alloc] initWithCustomView:leftButton];
self.navigationItem.leftBarButtonItem = leftItem; //①
//右边导航按钮
UIButton *rightButton = [[UIButton alloc] initWithFrame:CGRectMake(, , , )];
[rightButton setTitle:@"按钮" forState:UIControlStateNormal];
[rightButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
UIBarButtonItem *rightItem = [[UIBarButtonItem alloc] initWithCustomView:rightButton];
self.navigationItem.rightBarButtonItem = rightItem; UILabel *tipsLabel = [[UILabel alloc] initWithFrame:CGRectMake(, , self.view.frame.size.width-, )];
tipsLabel.text = @"我是子视图";
tipsLabel.textAlignment = NSTextAlignmentCenter;
tipsLabel.textColor = [UIColor blueColor];
[self.view addSubview:tipsLabel];
} - (void)clickedLeftButton
{
[self.navigationController popViewControllerAnimated:YES];
}

再说明下titleView的宽度问题

根视图中将labelView设置了500的宽度,而子视图中将labelView设置了10的宽度,但是显示效果是一样的,下面我们详细对比下这两种有什么不同。

我们可以看到,leftBarButtonItem距离左边距有16个像素,和titleView至少有6像素的距离;rightBarButtonItem同理。

所以我们假设屏幕宽度为ScreenW,标题文字的实际宽度为labelW,左边按钮宽度为leftW,右边按钮宽度为rightW,titleView的最终宽度为titleViewW。

①如果titleViewW < labelW,则titleViewW 为 labelW;

②如果labelW < titleViewW < ScreenW-16*2-6*2-leftW-rightW,则titleViewW 为 titleViewW;

③如果titleViewW > ScreenW-16*2-6*2-leftW-rightW,则titleViewW 为 ScreenW-16*2-6*2-leftW-rightW。

实际开发中可能会遇到标题过长,需要使用省略号“…”显示,如下图所示

此时让标题居中的办法就是将leftW和rightW设置为相等即可;

如果只有左边返回按钮,而没有右边按钮,则放置一个空视图占位,即可方便实现标题居中。

UINavigationController切换视图的简单使用的更多相关文章

  1. 九、UINavigationController切换视图 实例

    现版本 SDK 8.4 Xcode 运行Xcode 选择 Create a new Xcode project ->Single View Application 命名 NavigationCo ...

  2. swift:用UITabBarController、UINavigationController、模态窗口简单的搭建一个QQ界面

    搭建一个QQ界面其实是一个很简单的实现,需要几种切换视图的控制器组合一起使用,即导航控制器.标签栏控制器.模态窗口.其中,将标签栏控制器设置为window的rootViewController,因为Q ...

  3. vuejs切换视图同时保持状态

    vuejs切换视图同时保持状态 http://cn.vuejs.org/guide/components.html#动态组件 动态组件 多个组件可以使用同一个挂载点,然后动态地在它们之间切换.使用保留 ...

  4. iOS开发:使用Tab Bar切换视图

    iOS开发:使用Tab Bar切换视图 上一篇文章提到了多视图程序中各个视图之间的切换,用的Tool Bar,说白了还是根据触发事件使用代码改变Root View Controller中的Conten ...

  5. Android技术——切换视图(两)随着ViewPage达到Tab幻灯片浏览

    Android技术--切换视图(一)~(四)在资源项目:https://github.com/YongYuIT/MeiNv_Liulanqi 一.早期android(android.support.v ...

  6. 【转】Unity3D 场景切换与持久化简单数据储存(PlayerPrefs类)

    本篇文章主要介绍了"Unity3D 场景切换与持久化简单数据储存(PlayerPrefs类)",主要涉及到Unity3D 场景切换与持久化简单数据储存(PlayerPrefs类)方 ...

  7. viewSwitcher 切换视图

    通过VIewSwitcher切换视图.这个用到了baseAdapter,还是不太懂,先记个笔记. <RelativeLayout xmlns:android="http://schem ...

  8. CATransition(os开发之画面切换) 的简单用法

    CATransition 的简单用法 //引进CATransition 时要添加包“QuartzCore.framework”,然后引进“#import <QuartzCore/QuartzCo ...

  9. jquery写的tab切换效果 非常简单

    自己写的一款 tab切换效果,比较简单,适合新手 <style type="text/css">*{margin:0; padding:0; font-size:12p ...

随机推荐

  1. cocos2d-x 多线程以及线程同步

    转自:http://blog.csdn.net/zhy_cheng/article/details/9116479 cocos2d-x引擎在内部实现了一个庞大的主循环,每帧之间更新界面,如果耗时的操作 ...

  2. Hibernate征途(七)之复合主键映射和集合映射

    把这两种映射放到一起说,是因为这两种映射不像前面的复用型映射.数量和方向型映射那么分类鲜明,所以放到了这个“其他”里面. 复合主键映射 在关系模型中,复合主键和其他的主键方式没有很大区别,但是反映到对 ...

  3. ORACLE 11G用于有效期

    Oracle报错,ORA-28001: 口令已经失效(转自网络) 错误信息:ORA-28001: the password has expired解决方法 Oracle11G创建用户时缺省passwo ...

  4. JAVA数组的定义及用法

    数组是有序数据的集合,数组中的每一个元素具有同样的数组名和下标来唯一地确定数组中的元素. 1. 一维数组 1.1 一维数组的定义 type arrayName[]; type[] arrayName; ...

  5. VVDocumenter - Xcod代码注释工具

    刚接触IOS开发时,发现XCODE非常的强大的,后续的代码实践中发现XOCDE的代码文档注释非常的差, 每次都要用手敲,蛋疼至极: 随着不断学习发现XCODE有代码片段内嵌一说(如:for .bloc ...

  6. [Angular 2] Understanding Pure & Impure pipe

    First, how to use a build in pipe: <div class="pipe-example"> <label>Uppercase ...

  7. leetcode -- Largest Rectangle in Histogram TODO O(N)

    Given n non-negative integers representing the histogram's bar height where the width of each bar is ...

  8. 【张泽华】android视频教程下载地址及上课源代码

    http://note.youdao.com/share/?id=f39bf918842c7b0673a033d35d734073&type=notebook#/1AC357745ED74BC ...

  9. mysql自动备份策略

    目标:每7天做一个完整备份,每天做一份binlog日志,第二周将之前的备份删除并产生新的完整备份和binlog日志,备份要求每天2:00自动完成 mysql 版本:mysql5.5 1.开启binlo ...

  10. 那些著名或非著名的iOS面试题-前编

    1.如何追踪app崩溃率,如何解决线上闪退 当iOS设备上的App应用闪退时,操作系统会生成一个crash日志,保存在设备上.crash日志上有很多有用的信息,比如每个正在执行线程的完整堆栈跟踪信息和 ...