UINavigationControlle类详解


UINavigationController Class Reference

Inherits from

UIViewController:UIResponder:NSObject

Conforms to

NSCoding (UIViewController)

UIAppearanceContainer (UIViewController)

NSObject (NSObject)

Framework

/System/Library/Frameworks/UIKit.framework

Availability

Available in iOS 2.0 and later.

Companion guide

View Controller Catalog for iOS

Declared in

UINavigationController.h

Related sample code

AVPlayerDemo

ListAdder

MVCNetworking

TableSearch

TableViewSuite

UINavigationController总览

UINavigationController类是一个管理视图控制器内容分层的导航.navigation interface能更轻松有效地呈现你的数据并更加简单地对内容进行导航. This class is generally used as-is but may be subclassed in iOS 6 and later.

屏幕通过一个 navigation interface 来模拟数据的分层组织.在每一个层次中, 通过提供一个合适的屏幕 (通过自定义的试图控制其)来展示每个层次的内容.Figure 1是一个navigation interface的案例.首页展示给用户包含偏好的应用列表. 选中查看个人设置和应用设置.选中一个查看更多选项. 除了root view界面, navigation controller都会提供一个返回按钮供用户返回上一界面.

Figure 1一个简单的 navigation interface

navigation controller通过navigation stack管理当前显示的页面. 堆的底部是root view controller,堆的顶部时当前展示的 view controller. Navigation controller提供了在运行时修改stack的方法. 最常见的操作是使用pushViewController:animated:方法push一个新的view controllers到堆的顶层.在向navigation的堆上push一个视图控制器后,可以通过popViewControllerAnimated:方法来将顶部的试图控制其pop掉. pop操作可以主动触发,但navigation controller提供的默认返回按钮会自动执行pop操作.

navigation controller会通知它的delegate对象对活动的视图做出响应.delegate对象是应用程序中实现了UINavigationController协议的对象. 该接口定义的方法可用于响应变化并进行额外的操作

Navigation Controller Views

UINavigationController类继承自UIViewController类, navigation controllers可以通过view属性拥有自己的视图.但任一UINavigationController必须要有一个root view.可以通过rootViewController属性来指定.navigation controller的view是其他view容器 ,包括navigation bar, toolbar,和自定义的视图.

Figure 2展示了这些视图是图和被整合呈现在navigation interface中的.各部分视图的变化是独立的,例如toolBar视图的改变并不会影响到Custom content

Figure 2 navigation controller的视图层次

注意:由于自定义视图的空间可用数量可以改变(取决于navigation views的尺寸), 自定义视图应当拥有灵活的width和height.

navigation controller负责管理格局并显示navigation bar和navigation toolbar.直接修改这些views是不合法的,而应当通过UINavigationController类的方法和属性来操作.例如可以通过navigationBarHidden属性或setNavigationBarHidden:animated:方法设置navigation bar的显示或隐藏.

UINavigationController提供了一些方法来定制navigation bar的显示.UINavigationController允许修改BarStyle或者translucent属性但绝不要直接修改它的frame,bounds, 或者alpha. 你可以通过initWithNavigationBarClass:toolbarClass:创建一个的UINavigationBar的子类并对navigation controller初始化.此外,可以通过navigation items(UINavigationItem类的实例)动态创建关联于视图控制器的navigation bar. 为了改变navigation bar的内容,自定义的view controllers必须设置navigation items.

Updating the Navigation Bar

无论是通过push还是pop改变了顶层的view controller,或者直接改变了堆的内容,navigation controller都会自动更新navigation bar. navigation controller会更新navigation bar左,中,右三个位置的bar button items. Bar button items 是UIBarButtonItem类的实例,可以根据需要自定义 items,当然也可以使用系统自带的 items.navigation bar左边的bar button item 通常用于可以返回上一界面.

左侧位置的 navigation bar的更新规则如下:

  • 当顶层视图存在自定义的left bar button item,则显示自定义按钮,可通过leftBarButtonItem属性对其进行设置.
  • 如果顶部视图没有自定义的 left bar button item,上一视图设定了backBarButtonItem,则显示此按钮并将上一个view的title赋值给该按钮的title属性
  • 当上述两者要求均不符合时,navigation bar就会显示一个包含视图标题的label,要想显示不同的文字可通过修改视图的navigation item的title属性

中间位置的 navigation bar的更新规则如下:

  • 如果顶层视图有自定义的view,则以自定义view取代默认title view.
  • 如果顶层 视图没有自定义的titleview,navigation bar便在title view位置显示一个label.label的字符串为该视图的titlez值.如果想显示一个不同的title,可设置title属性

右边位置的 navigation bar的更新规则如下:

  •如果顶层视图指定了right bar button item便显示,可以通过rightBarButtonItem属性设置

  • 如果顶层视图没有指定的 right bar button item则不显示

每当顶部视图改变时navigation controller都会更新navigation bar.因此每次视图被push或pop的时, navigation controller也同时在动态改变navigation bar的内容.

Displaying a Toolbar

iOS 3.0以后, navigation controller 对象可以为每个视图添加自定义的toolbar. navigation controller对象可以在视图中控制一个toolbar的显示与隐藏. 当需要显示时, toolbar根据当前活动的视图的toolbarItems属性获得. 当活动视图改变时, navigation controller会更新 toolbar items以适应新的 view controller.

navigation toolbar默认为隐藏,但可以通过调用setToolbarHidden:animated:方法将其显示.如果不是所有视图需要显示toolbar items,则delegate对象可以在随后的push和pop操作中通过回调这个方法来改变toolbar的能见度.通过initWithNavigationBarClass:toolbarClass:方法来初始化 navigation controller可显示自定义的UIToolbar.

State Preservation

iOS 6以后, 当为navigation controller的restorationIdentifier属性赋值时,navigation controller的所有拥有合法restoration identifie的子视图都会被编码. 在下一次启动周期中, navigation controller根据视图的保存顺序将其恢复到 navigation堆中.通过push操作的子视图也可以使用相同的restoration identifiers. navigation controller自动保存额外信息以确保每个子类的恢复信息是唯一的.

Tasks

Creating Navigation Controllers

  • –initWithRootViewController:
  • –initWithNavigationBarClass:toolbarClass:

Accessing Items on the Navigation Stack

  • topViewControllerproperty
  • visibleViewControllerproperty
  • viewControllersproperty
  • –setViewControllers:animated:

Pushing and Popping Stack Items

  • –pushViewController:animated:
  • –popViewControllerAnimated:
  • –popToRootViewControllerAnimated:
  • –popToViewController:animated:

Configuring Navigation Bars

  • navigationBarproperty
  • navigationBarHiddenproperty
  • –setNavigationBarHidden:animated:

Accessing the Delegate

  • delegateproperty

Configuring Custom Toolbars

  • toolbarproperty
  • –setToolbarHidden:animated:
  • toolbarHiddenproperty

Properties

delegate

The receiver’s delegate ornilif it doesn’t have a delegate.

@property(nonatomic, assign) id<UINavigationControllerDelegate> delegate

Discussion

SeeUINavigationControllerDelegate Protocol Referencefor the methods this delegate should implement.

Availability

  • Available in iOS 2.0 and later.

Declared In

UINavigationController.h

navigationBar

The navigation bar managed by the navigation controller. (read-only)

@property(nonatomic, readonly)UINavigationBar*navigationBar

Discussion

It is permissible to modify thebarStyleortranslucentproperties of the navigation bar but you must never change itsframe,bounds, oralphavalues directly. To show or hide the navigation bar, you should always do so through the navigation controller by changing itsnavigationBarHiddenproperty or calling thesetNavigationBarHidden:animated:method.

Availability

  • Available in iOS 2.0 and later.

Related Sample Code

  • NavBar
  • Tabster

Declared In

UINavigationController.h

navigationBarHidden

A Boolean value that determines whether the navigation bar is hidden.

@property(nonatomic, getter=isNavigationBarHidden) BOOL navigationBarHidden

Discussion

IfYES, the navigation bar is hidden. The default value isNO. Setting this property does not animate the hiding or showing of the navigation bar; usesetNavigationBarHidden:animated:for that purpose.

Availability

  • Available in iOS 2.0 and later.

Related Sample Code

  • AVPlayerDemo

Declared In

UINavigationController.h

优质内容筛选与推荐>>
1、Wannafly挑战赛5
2、java程序性能分析之thread dump和heap dump
3、数据结构03 线性表之链表
4、关于IT/Python学习:写在前面
5、十六.golang的结构体


长按二维码向我转账

受苹果公司新规定影响,微信 iOS 版的赞赏功能被关闭,可通过二维码转账支持公众号。

    阅读
    好看
    已推荐到看一看
    你的朋友可以在“发现”-“看一看”看到你认为好看的文章。
    已取消,“好看”想法已同步删除
    已推荐到看一看 和朋友分享想法
    最多200字,当前共 发送

    已发送

    朋友将在看一看看到

    确定
    分享你的想法...
    取消

    分享想法到看一看

    确定
    最多200字,当前共

    发送中

    网络异常,请稍后重试

    微信扫一扫
    关注该公众号