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 ...
随机推荐
- 移植FreeModbus+ModbusMaster+STM32至RT-Thread(初步)
一.项目描述 目前操作系统在嵌入式软件行业非常流行,在工控组网方面,以后可能会经常使用到Modbus主机+操作系统.Modbus从机+操作系统甚至Modbus主机+Modbus从机+操作系统.但是操作 ...
- synthesize(合成) keyword in IOS
synthesize creates setter and getter (从Objective-C 2.0开始,合成可自动生成存取方法) the setter is used by IOS to s ...
- 一个简单的小例子让你明白c#中的委托-终于懂了!
模拟主持人发布一个问题,由多个嘉宾来回答这个问题. 分析:从需求中抽出Host (主持人) 类和Guests (嘉宾) 类. 作为问题的发布者,Host不知道问题如何解答.因此它只能发布这个事件,将事 ...
- 线程池QueueUserWorkItem
// Test1.cpp : Defines the entry point for the console application. // #include "stdafx.h" ...
- [AngualrJS] ng-strict-di
In Angular 1.5 introduces "compoment" syntax. But ng-annotate doesn't understand ".co ...
- Node.js 的Web server--Fenix
Fenix 是提供给开发者使用的简单的一个 Web server, 是基于 Node.js 开发. 能够同一时候在上面执行非常多的项目. 最适合前端开发者使用. 能够通过免费的 Node.js 控制台 ...
- 使用python selenium进行自动化functional test
Why Automation Testing 现在似乎大家都一致认同一个项目应该有足够多的测试来保证功能的正常运作,而且这些此处的‘测试’特指自动化测试:并且大多数人会认为如果还有哪个项目依然采用人工 ...
- SAP SOAMANAGER 配置WEBSERVICE 提示:Service cannot be reached解决方法
TM中有些服务没有被激活,以UI界面个性化设置化设置为例: 如果服务没有被激活,打开界面就会显示: 这时候右键点击属性,获取服务ID: 通过事务代码SICF,输入服务ID:wd_analyze_con ...
- springMVC2 1入门程序
1入门程序 .1需求 实现商品列表查询 .2需要的jar包 使用spring3.2.0(带springwebmvc模块) .1前端控制器 在web.xml中配置: <?xml version=& ...
- NGUI panel使用soft clip时,屏幕缩放后无法正常工作的问题解决
最近开始使用NGUI,通过查找,搞定了屏幕缩放问题,但在用到panel的soft clip时,碰到了问题,NGUI给出了警告 “clipped panels must have a uniform s ...