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. Eclipse10大快捷键组合

    一个Eclipse骨灰级开发者总结了他认为最有用但又不太为人所知的快捷键组合.通过这些组合可以更加容易的浏览源代码,使得整体的开发效率和质量得到提升. Ctrl+Shift+C 快速单行注释 也适用于 ...

  2. Android如何判断一个应用在运行

    在一个应用中,或一个Service .Receiver中有时候需要判断一个应用是否正在运行,以便进行一些相关的处理,这个时候我们需要得到一个ActivityManager,这个Manager顾名思意就 ...

  3. 嗯,记录一些eclipse的快捷键

    alt+/:自动补全 ctrl+/:注释 // 再按一下取消注释 ctrl+shift+\:区块注释 /* */ ctrl+shift+\:取消区块注释 ctrl+shift+f:格式化代码 ctrl ...

  4. BW知识点总结及面试要点

    1.       如何理解数据仓库? 数据仓库 是 一个面向主题的,集成的,相对稳定的,反应历史变化的数据集合,用于支持管理决策. 2.       OLAP 和 OLTP的基本概念 和 区别? Ol ...

  5. Educational Codeforces Round 2 C. Make Palindrome 贪心

    C. Make Palindrome Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/600/pr ...

  6. EasyUI改动DateBox和DateTimeBox的默认日期格式

    近期整理Easyui控件的时候,对Easyui的DateBox控件和DateTimeBox控件进行了梳理,而我之所以将EasyUI的DateBox控件和DateTimeBox控件放在一起,归为一类,是 ...

  7. [Javascript] Either Functor

    Either Functor: // API Right(val) // resolve the value Left(val) // return error message Examples: m ...

  8. Tomcat安装、配置、优化及负载均衡详解

    一.常见JavaWeb服务器      1.WebLogic:是BEA公司的产品.WebSphereAS:是IBM公司的产品.JBossAS:红帽公司的产品,可以自行了解      2.Tomcat服 ...

  9. MySQL并发复制系列三:MySQL和MariaDB实现对比

    http://blog.itpub.net/28218939/viewspace-1975856/ 并发复制(Parallel Replication) 系列三:MySQL 5.7 和MariaDB ...

  10. Java_maven构建项目(多模块项目)

    在eclipse下构建maven项目,该项目由多个子模块组成. 1.创建一个父项目 NEW -->project-->maven-->maven Project,点击下一步,进入ne ...