UINavigationController切换视图的简单使用
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切换视图的简单使用的更多相关文章
- 九、UINavigationController切换视图 实例
现版本 SDK 8.4 Xcode 运行Xcode 选择 Create a new Xcode project ->Single View Application 命名 NavigationCo ...
- swift:用UITabBarController、UINavigationController、模态窗口简单的搭建一个QQ界面
搭建一个QQ界面其实是一个很简单的实现,需要几种切换视图的控制器组合一起使用,即导航控制器.标签栏控制器.模态窗口.其中,将标签栏控制器设置为window的rootViewController,因为Q ...
- vuejs切换视图同时保持状态
vuejs切换视图同时保持状态 http://cn.vuejs.org/guide/components.html#动态组件 动态组件 多个组件可以使用同一个挂载点,然后动态地在它们之间切换.使用保留 ...
- iOS开发:使用Tab Bar切换视图
iOS开发:使用Tab Bar切换视图 上一篇文章提到了多视图程序中各个视图之间的切换,用的Tool Bar,说白了还是根据触发事件使用代码改变Root View Controller中的Conten ...
- Android技术——切换视图(两)随着ViewPage达到Tab幻灯片浏览
Android技术--切换视图(一)~(四)在资源项目:https://github.com/YongYuIT/MeiNv_Liulanqi 一.早期android(android.support.v ...
- 【转】Unity3D 场景切换与持久化简单数据储存(PlayerPrefs类)
本篇文章主要介绍了"Unity3D 场景切换与持久化简单数据储存(PlayerPrefs类)",主要涉及到Unity3D 场景切换与持久化简单数据储存(PlayerPrefs类)方 ...
- viewSwitcher 切换视图
通过VIewSwitcher切换视图.这个用到了baseAdapter,还是不太懂,先记个笔记. <RelativeLayout xmlns:android="http://schem ...
- CATransition(os开发之画面切换) 的简单用法
CATransition 的简单用法 //引进CATransition 时要添加包“QuartzCore.framework”,然后引进“#import <QuartzCore/QuartzCo ...
- jquery写的tab切换效果 非常简单
自己写的一款 tab切换效果,比较简单,适合新手 <style type="text/css">*{margin:0; padding:0; font-size:12p ...
随机推荐
- 极域电子教室 e-Learning Class V4 2010专业版 学生机 卸载方法
学校的机房一般都会装教师机远程控制学生机的软件.比如我们学校装的就是"极域电子教室 e-Learning Class V4 2010专业版",上课的时候直接强制全屏远控,卸载又提示 ...
- linux自己带的apache重新启动
如果是linux自己带的apache的话就使用命令 service httpd start 启动 service httpd stop 关闭 service httpd restart 重新启动 如果 ...
- 使用C#开发纽曼USB来电小秘书客户端小结
在前面用C#开发完CRM的来电弹屏之后,有些客户有了新的要求,他们希望不但能够实现来电弹屏,更希望能够将呼入呼出的电话录音并上传到CRM服务器上,方便日后跟踪记录.于是便有了来电小秘书客户端的开发. ...
- 【转】VIM 快速注释
我是用自己自定义的,跟你分享一下吧.希望能帮到你. 在.vimrc中加入下面的语句:vmap <C-S-P> dO#endif<Esc>PO#if 0<Esc> ...
- 用实例展示left Join,right join,inner join,join,cross join,union 的区别
1.向TI,T2插入数据: T1 7条 ID Field2 Field3 Field41 1 3 542 1 3 543 1 3 544 2 3 545 3 3 546 4 3 547 5 3 54 ...
- 决策树算法实现(train+test,matlab) 转
原文:http://www.zgxue.com/198/1985544.html 华电北风吹 天津大学认知计算与应用重点实验室 修改日期:2015/8/15 决策树是一种特别简单的机器学习分类算法.决 ...
- centos中samba配置后始终连不上的绝招
奶奶的,按照网上的办法改了/etc/samba/smb.conf配置文件,结果在win7里面死活连不上,后来发现是防火墙的问题. 有两个命令必须要执行: 1.将SELIUNX设置成disabled或者 ...
- 关于Servlet会话跟踪的那些事儿
关于servlet会话跟踪,一搜都能搜出很多.我也不免落入俗套,也总结了一把.希望我所总结的知识尽量是知识海洋里的一汪清泉.能帮助到我自己和哪怕一个人,那也是值得的. 故事由来: 我们知道,http协 ...
- ASP.NET MVC and jqGrid 学习笔记 5-添加数据
据介绍,jqgrid有3种编辑数据的方式: Cell Editing 只允许修改某一个单元格内容 Inline Editing 允许在jqGrid中直接修改某一行的数据 Form Editing 弹出 ...
- Java作业代写
作业一 试用java编写一个九九乘法表并打印. 作业二: 设计两个人类与书类,并设置两者的关系,试用人去找书,书去找人,假如某人有一个儿子,它也有一本书,试用儿子去找书,书找儿子. 大作业 熟悉QQ农 ...