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/8480048 图形绘制的话,在cocos2d-x自带的TestCpp里有,包括绘制点,直线,多边形 ...

  2. PostgreSQL建表动作分析

    首先,建立表: pgsql=# create table tab10(id integer); CREATE TABLE pgsql::regclass; regclass ---------- ta ...

  3. VHD更新命令(打补丁)

    DISM 查看vhd文件信息:dism /get-imageinfo /imagefile:e:\vhd\win2008r2.vhdxdism /get-imageinfo /imagefile:e: ...

  4. 定义member【C++】cstddef中4个定义

    最近研究定义member,稍微总结一下,以后继续补充: size_t size_t corresponds to the integral data type returned by the lang ...

  5. C# 使用微软的Visual Studio International Pack 类库提取汉字拼音首字母

    首先下载Visual Studio International Pack 1.0,官方下载地址:http://www.microsoft.com/downloads/zh-cn/details.asp ...

  6. C# 中的 lock的陷阱

    旧事重提了,或许很多人会奇怪,为什么 C# 不允许lock一个struct ? 例如: public void ProcessTask(int taskid){     lock(taskid){  ...

  7. GB2312 Unicode转换表实现跨平台utf8转码unicode

    在GSM模块中,为发送中文短信,采用pdu发送,需要unicode编码.源程序编辑软件将中文转化为GB2312编码,再下列的GB2312 Unicode转换表转为unicode. 实现2维数值,GB2 ...

  8. Lua学习教程之 可变參数数据打包与解包

    利用table的pack与unpack进行数据打包与解包.測试代码例如以下: print("Test table.pack()----------------"); functio ...

  9. asp.net获取客户端IP,Mac

    public class GetClientInfo { [DllImport("Iphlpapi.dll")] private static extern int SendARP ...

  10. 如何获取input自定义属性

    javascript方法: <input type="checkbox"  name="sdf"  data="这是自定义属性" /& ...