您的位置:首页 > 产品设计 > UI/UE

UINavigationController 学习摘要

2016-07-01 13:56 399 查看
UINavigationController学习摘要
UINavigagtionController负责管理多个UIViewController之间的水平方向的push(压入)和pop(弹出),并同步修改它一个子视图:navigationBar,即,我们常说的导航条。

我们可以看一下它的结构图,从结构图中,我们看到,它有一个NSArray的数据成员,即它管理的视图栈。





UINavigationController 是继承自UIViewController 的子类,也有自己的视图,它至少有3个子视图:navigationBar、topViewController以及toolBar。我们首先看一下苹果帮助文档中的结构图:

 


下面我们重点讲一下navigationBar和toolBar的工作原理

1、navigationBar

它位于整个视图的顶部。讲到它,我们先查一下UIViewController对象中一个navigationTtem 属性,类型为 UINavigationItem,它和UINavigationBar 不同,UINavigationItem 不是UIView 子类,不能在屏幕上 显示,主要是为 UINavigationBar 对象提供绘图所需的内容。当某一个UIViewController 对象为 UINavigationController 的栈顶对象时,UINavigationBar
对象会使用UIViewController 对象中 navigationItem 并设置相应的属性。

UINavigationBar和UINavigationItem这两者之间的关系图:



UINavigationItem 包括 4 个部分的可以自定义的部分:leftBarButtonItem、rightBarButtonItem
、backBarButtonItem以及titleView。他们都是指针,前三者都指向UIBarButtonItem  实例(此类按钮只能在UINavigationBar 或 UIToolBar 上显示)也不是UIView 子类,只提供 UINavigationBar 绘图所需的内容。

其中 backBarButtonItem 自定义比较的特殊:研读 UINavigationControllerClass Reference 中,在“Updating the Navigation Bar” 小节,有这么一段话:

The bar button item on the left sideof the navigation bar allows for navigation back to the previous viewcontroller on the navigation stack. The navigationcontroller
updates the leftside of the navigation bar as follows:

• If the new top-level viewcontroller has a custom left bar button item, that item is displayed. Tospecify a custom left bar button item, set the leftBarButtonItem property ofthe view controller’s
navigation item.

• If the top-level view controllerdoes not have a custom left bar button item, butthe navigation item of thepreviousview controllerhas
a valid item in its backBarButtonItem property,the navigation bar displays that item.

• If a custom bar button item is notspecified by either of the view controllers, a default back button is used andits title is set to the value of the title property of the previous viewcontroller—
that is, the view controller one level down on the stack. (If thereis only one view controller on the navigation stack, no back button isdisplayed.)

从上面的官方解释来看,使用 pushViewController 切换到下一个视图时,navigation controller 按照以下3 条顺序更改导航栏的左侧按钮:

1)、如果 B 视图有一个自定义的左侧按钮(leftBarButtonItem),则会显示这个 自定义按钮;

2)、如果 B 没有自定义按钮,但是 A 视图的 backBarButtonItem 属性有自定义项, 则显示这个自定义项;

3)、如果前 2 条都没有,则默认显示一个后退按钮,后退按钮的标题是A 视图的标题。

从第二点来看,我们可以在A视图中自定义back按钮,示例代码如下:

UIBarButtonItem *backItem = [[UIBarButtonItemalloc]

initWithTitle:@"返回"style:UIBarButtonItemStyleBordered

target:nil action:nil];

[self.navigationItem setBackBarButtonItem:backItem];

[backItem release];

经过测试验证,指定事件并也不会起作用,需要继续研究,后期可以补充。

2、toolBar

与navigationBar类似,在UIViewController对象中也有一个toolBarItems的属性,用于定义toolBar上显示的内容,我们可以看下面的结构图:



值得注意的是,默认情况下是不显示toolBar的,需要调用UINavigationController对象的的-(void)setToolbarHidden:(BOOL)hiddenanimated:(BOOL)animated

方法来显示和隐藏toolBar。一般是采用设置UIViewController的hidesBottomBarWhenPushed的值为YES,当push或者pop时,自动隐藏底部的toolBar。

 

 

 

 
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息