iOS 之 UINavigationController 记录

时间:2022-12-09 08:04:12

有一阵子没有写随笔,感觉有点儿手生。一个多月以后终于又一次坐下来静下心写随笔,记录自己的学习笔记,也希望能够帮到大家。

  废话少说回到正题,UINavigationController是IOS编程中比较常用的一种容器view controller,很多系统的控件(如UIImagePickerViewController)以及很多有名的APP中(如qq,系统相册等)都有 用到。说是使用详解,其实我只会介绍几个自认为比较重要或者容易放错的地方进行讲解,下面让我们挨个探探究竟:
     UINavigationController是用于构建分层应用程序的主要工具,主要采用栈形式来实现视图。
任何类型的视图控制器都可放入栈中。在设计导航控制器时需要指定根视图即用户看到的第一个视图。根视图控制器是被导航控制器推入到栈中的第一个视图控制
器。当用户查看下一个试图时,栈中将加入一个新的视图控制器,它所控制的视图将展示给用户。我们可以通过导航按钮来操作分层的应用程序,用它来控制视图的
推入或推出。

  首先上一张图(来自苹果官方文档):

iOS 之 UINavigationController 记录

                        UINavigationController view层级

1、navigationItem

  我们都知道navigationItem是UIViewController的一个属性,这个属性是为
UINavigationController服务的。文档中是这么解释的“The navigation item used to
represent the view controller in a parent’s navigation bar.
(read-only)”,即navigation item在navigation
Bar代表一个viewController,具体一点儿来说就是每一个加到navigationController的viewController都
会有一个对应的navigationItem,该对象由viewController以懒加载的方式创建,稍后我们可以在对象中堆
navigationItem进行配置,可以设置leftBarButtonItem, rightBarButtonItem,
backBarButtonItem,
title以及prompt等属性。前三个每一个都是一个UIBarButtonItem对象,最后两个属性是一个NSString类型描述,注意添加该
描述以后NavigationBar的高度会增加30,总的高度会变成74(不管当前方向是Portrait还是Landscape,此模式下
navgationbar都使用高度44加上prompt30的方式进行显示)。当然如果觉得只是设置文字的title不够爽,你还可以通过
titleview属性指定一个定制的titleview,这样你就可以随心所欲了,当然注意指定的titleview的frame大小,不要显示出界。

  举个简单的例子:

iOS 之 UINavigationController 记录
iOS 之 UINavigationController 记录
// set rightItem
UIBarButtonItem *rightItem = [[UIBarButtonItem alloc] initWithTitle:@"Root" style:UIBarButtonItemStyleBordered target:self action:@selector(popToRootVC)];
childOne.navigationItem.rightBarButtonItem = rightItem;
[rightItem release]; // when you design a prompt for navigationbar, the hiehgt of navigationbar will becaome 74, ignore the orientation
childOne.navigationItem.prompt = @"Hello, im the prompt";
iOS 之 UINavigationController 记录
iOS 之 UINavigationController 记录

  这段代码设置了navigationItem的rightBarButtonItem,同时设置了prompt信息。

 

2、titleTextAttributes(ios5.0以后可用)

  这是UINavigationBar的一个属性,通过它你可以设置title部分的字体,这个属性定义如下:

/* You may specify the font, text color, text shadow color, and text shadow offset for the title in the text attributes dictionary, using the keys found in UIStringDrawing.h.
*/
@property(nonatomic,copy) NSDictionary *titleTextAttributes __OSX_AVAILABLE_STARTING(__MAC_NA,__IPHONE_5_0) UI_APPEARANCE_SELECTOR;

  它的dictionary的key定义以及其对应的value类型如下:

//    Keys for Text Attributes Dictionaries
// NSString *const UITextAttributeFont; value: UIFont
// NSString *const UITextAttributeTextColor; value: UIColor
// NSString *const UITextAttributeTextShadowColor; value: UIColor
// NSString *const UITextAttributeTextShadowOffset; value: NSValue wrapping a UIOffset struct.

  下面看一个简单的例子:

NSDictionary *dict = [NSDictionary dictionaryWithObject:[UIColor yellowColor] forKey:UITextAttributeTextColor];
childOne.navigationController.navigationBar.titleTextAttributes = dict;

  这个例子就是设置title的字体颜色为黄色,怎么样简单吧。

 

3、wantsFullScreenLayout

  viewController的一个属性,这个属性默认值是NO,如果设置为YES的话,如果 statusbar,navigationbar, toolbar是半透明的话,viewController的view就会缩放延伸到它们下面,但注意一点儿tabBar不在范围内,即无论该属性是否为 YES,view都不会覆盖到tabbar的下方。

 

4、navigationBar中的stack

  这个属性可以算是UINavigationController的灵魂之一,它维护了一个和UINavigationController中 viewControllers对应的navigationItem的stack,该stack用于负责navigationbar的刷新。“注意:如果 navigationbar中navigationItem的stack和对应的NavigationController中 viewController的stack是一一对应的关系,如果两个stack不同步就会抛出异常。

  下面举个简单抛出异常的例子:

SvNavChildViewController *childOne = [[SvNavChildViewController alloc] initWithTitle:@"First" content:@"1"];
[self.navigationController pushViewController:childOne animated:NO];
[childOne release]; // raise exception when the stack of navigationbar and navigationController was not correspond
[self.navigationController.navigationBar popNavigationItemAnimated:NO];

  当pushViewcontroller的之后,强制把navigationBar中的navigationItem pop一个出去,程序立马挂起。当然这纯粹只是为了验证问题,我想一般的码农没有人会这么写的。

 

5、navigationBar的刷新

  通过前面介绍的内容,我们知道navigationBar中包含了这几个重要组成部 分:leftBarButtonItem, rightBarButtonItem, backBarButtonItem, title。当一个view controller添加到navigationController以后,navigationBar的显示遵循一下几个原则:

  1)、Left side of the navigationBar

  a)如果当前的viewController设置了leftBarButtonItem,则显示当前VC所自带的leftBarButtonItem。

  b)如果当前的viewController没有设置leftBarButtonItem,且当前VC不是rootVC的时候,则显示前一层 VC的backBarButtonItem。如果前一层的VC没有显示的指定backBarButtonItem的话,系统将会根据前一层VC的 title属性自动生成一个back按钮,并显示出来。

  c)如果当前的viewController没有设置leftBarButtonItem,且当前VC已是rootVC的时候,左边将不显示任何东西。

  此处注意:5.0中新增加了一个属性leftItemsSupplementBackButton,通过指定该属性为YES,可以让 leftBarButtonItem和backBarButtonItem同时显示,其中leftBarButtonItem显示在 backBarButtonItem的右边。

  2)、title部分

  a)如果当前VC通过 .navigationItem.titleView指定了自定义的titleView,系统将会显示指定的titleView,此处要注意自定义titleView的高度不要超过navigationBar的高度,否则会显示出界。

  b)如果当前VC没有指定titleView,系统则会根据当前VC的title或者当前VC的navigationItem.title的内 容创建一个UILabel并显示,其中如果指定了navigationItem.title的话,则优先显示navigationItem.title的 内容。

  3)、Right side of the navigationBar

  a)如果当前VC指定了rightBarButtonItem的话,则显示指定的内容。

  b)如果当前VC没有指定rightBarButtonItem的话,则不显示任何东西。

 

6、Toolbar

  navigationController自带了一个工具栏,通过设置 self.navigationController.toolbarHidden = NO来显示工具栏,工具栏中的内容可以通过viewController的toolbarItems来设置,显示的顺序和设置的NSArray中存放的顺 序一致,其中每一个数据都一个UIBarButtonItem对象,可以使用系统提供的很多常用风格的对象,也可以根据需求进行自定义。

  设置Toolbar内容的例子:

iOS 之 UINavigationController 记录
iOS 之 UINavigationController 记录
// test ToolBar
UIBarButtonItem *one = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAdd target:nil action:nil];
UIBarButtonItem *two = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemBookmarks target:nil action:nil];
UIBarButtonItem *three = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAction target:nil action:nil];
UIBarButtonItem *four = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemEdit target:nil action:nil];
UIBarButtonItem *flexItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil];
[childOne setToolbarItems:[NSArray arrayWithObjects:flexItem, one, flexItem, two, flexItem, three, flexItem, four, flexItem, nil]];
[one release];
[two release];
[three release];
[four release];
[flexItem release]; childOne.navigationController.toolbarHidden = NO;
iOS 之 UINavigationController 记录
iOS 之 UINavigationController 记录

 

7、UINavigationControllerDelegate

  这个代理真的很简单,就是当一个viewController要显示的时候通知一下外面,给你一个机会进行设置,包含如下两个函数:

// Called when the navigation controller shows a new top view controller via a push, pop or setting of the view controller stack.
- (void)navigationController:(UINavigationController *)navigationController willShowViewController:(UIViewController *)viewController animated:(BOOL)animated;
- (void)navigationController:(UINavigationController *)navigationController didShowViewController:(UIViewController *)viewController animated:(BOOL)animated;

  当你需要对某些将要显示的viewController进行修改的话,可实现该代理。

 

8、UINavigationController的viewControllers属性

  通过该属性我们可以实现一次性替换整个navigationController的层次, 这个过程如果通过setViewControllers:animated:来设置,并指定动画为YES的画,动画将会从当前的navigationController所显示的vc跳转到所设置的目标viewController的最顶层的那个VC,而中间其他的VC将会被直接从VC层级中移除和添加进来(没有动画)。

 

9、topViewController Vs visibleViewController

  topViewController代表当前navigation栈中最上层的VC,而visibleViewController代表当前可
见的VC,它可能是topViewController,也可能是当前topViewController
present出来的VC。因此UINavigationController的这两个属性通常情况下是一样,但也有可能不同。

copy from http://www.cnblogs.com/martin1009/archive/2012/05/30/2526392.html

iOS 之 UINavigationController 记录的更多相关文章

  1. iOS开发之记录用户登录状态

    iOS开发之记录用户登录状态 我们知道:CoreData的配置和使用步骤还是挺复杂的.但熟悉CoreData的使用流程后,CoreData还是蛮好用的.今天要说的是如何记录我们用户的登陆状态.例如微信 ...

  2. IOS开发之记录用户登陆状态,ios开发用户登陆

    IOS开发之记录用户登陆状态,ios开发用户登陆 上一篇博客中提到了用CoreData来进行数据的持久化,CoreData的配置和使用步骤还是挺复杂的.但熟悉CoreData的使用流程后,CoreDa ...

  3. iOS Programming UINavigationController

    iOS Programming UINavigationController the Settings application has multiple related screens of info ...

  4. iOS 的UINavigationController详解与使用添加UIBarButtonItem

    转发自:http://blog.csdn.net/totogo2010/article/details/7681879 分类: iOS开发入门2012-06-21 11:10 53077人阅读 评论( ...

  5. iOS开发-UINavigationController简单介绍

    导航条或者说导航栏目现在在App中基本上也算是标配,类似于父子级别的味道在里面,UINavigationController就是负责简化这一实现功能的,属于iOS开发中比较常用的一种容器View co ...

  6. Github上的iOS资料-个人记录

    动画 awesome-ios-animation收集了iOS平台下比较主流炫酷的几款动画框架 RCTRefreshControlqq的下拉刷新 TBIconTransitionKiticon 的点击动 ...

  7. IOS开发之记录用户登陆状态

    上一篇博客中提到了用CoreData来进行数据的持久化,CoreData的配置和使用步骤还是挺复杂的.但熟悉CoreData的使用流程后,CoreData还是蛮好用的.今天要说的是如何记录我们用户的登 ...

  8. iOS运用fabric记录crash日志过程

    先前运用友盟记录app闪退,发现有些闪退的记录无法明确定位到详细的位置,决定运用fabric进行闪退的记录:网上也有这方面的记录,有些细节的内容不明确,把今天碰到的坑整理记发不一下: 访问官网地址(进 ...

  9. iOS中UINavigationController控制器使用详解

    一.概述 UINavigationController用来管理视图控制器,在多视图控制器中常用.它以栈的形式管理视图控制器,管理视图控制器个数理论上不受限制(实际受内存限制),push和pop方法来弹 ...

随机推荐

  1. 在Sublime TEXT3中添加brogrammer-theme主题配色方案

    喜欢用sublime text3的都知道原配的主题要么就是背景颜色太亮太累眼,要么就是配色太少不便于一眼看出哪有问题.所以在此推荐一个brogrammer-theme的主题,配色非常的全而且添加了扁平 ...

  2. HTML5 WebStorage

    WebStorage是HTML5中本地存储的解决方案之一,在HTML5的WebStorage概念引入之前除去IE User Data.Flash Cookie.Google Gears等看名字就不靠谱 ...

  3. Domain Driven Design and Development In Practice--转载

    原文地址:http://www.infoq.com/articles/ddd-in-practice Background Domain Driven Design (DDD) is about ma ...

  4. 从零开始学ios开发(十九):Application Settings and User Defaults(上)

    在iphone和ipad中,有一个东西大家一定很熟悉,那个东西就是Settings. 这次要学习的东西说白了很简单,就是学习如何在Settings中对一个app的某些属性进行设置,反过来,在app中更 ...

  5. php验证输入字符串中含有非法字符

    $pattern = "/(&|"|<|>|')+/";  preg_match($pattern, $media_name, $matches);  ...

  6. 理解Python的&ast;args&comma; &ast;&ast;kwargs

    1 # coding=utf-8 2 def speak(*args, **kwargs): 3 print args, kwargs 4 5 6 a = 1 7 b = 2 8 c = 3 9 sp ...

  7. java 移位运算

    移位运算 :将整数转化为二进制(以补码的形式),按位平移. <<     左移 >>     右移 >>>   无符号右移 << 右移: 按位做平 ...

  8. 41&period;Linux应用调试-修改内核来打印用户态的oops

    1.在之前第36章里,我们学习了通过驱动的oops定位错误代码行 第36章的oops代码如下所示: Unable to handle kernel paging request at //无法处理内核 ...

  9. chrome disable-web-security 关闭安全策略 解决跨域

    Chrome 跨域访问线上接口 时间:2016-04-21 作者:zhongxia 前后端分离之后,联调的时候就会出现问题,那就是Ajax跨域问题. 跨域问题的解决方案有很多种比如常规的 后端使用CR ...

  10. IDEA打开maven项目dependencies红线

    第一步:install报红的项目,从maven库下载需要的包,看看日志还缺哪些本地包,少了就去下,丢到库里.不缺包后,reimport一下一般就OK了,如果还是红的,重启一下就好了. 如果第一步还没好 ...