这从一些关于导航器的重要信息的官方文档文章摘编都翻译。便于参考。
The
UINavigationController
class implements a specialized view controller that manages the navigation of hierarchical content. UINavigationController
类为管理具有一定层次结构的内容实现了专门的视图控制器。 A navigation controller object manages the currently displayed screens using the navigation stack , which is represented by an array of view controllers. The first view controller in the array corresponds to the root view controller. The last view controller in the array represents the view controller currently being displayed.
导航控制器对象使用 导航栈管理当前屏幕显示的内容。它通过一个视图控制器数组来展现这些内容。数组中的第一个视图控制器相应导航控制器的根视图控制器。
数组的最后一个视图控制器表示当前显示的视图控制器。
Typically, you add a view controller to the top of the stack using the
method. Pushing a view controller displays its view in the navigation interface and updates the navigation controls accordingly. 通常。加入一个视图控制器到栈顶使用:
方法。当push一个视图控制器时,视图控制器会将它自己的视图显示到导航界面并更新对应的导航控制。 To remove a view controller from the stack programmatically, you call the
method. 使用编程方式从导航栈中移除一个视图控制器,你能够调用:
方法。 A navigation controller object tells its delegate about changes to the active view controller. You can assign a delegate to your navigation controller if you want to perform additional setup or cleanup tasks related to the pushing and popping of view controllers. The delegate object you provide must conform to the
protocol. 当活动的视图控制器发生变化时,导航控制会把这些变化通知给它的托付对象。
假设你想运行一些附加的设置或者清理一些与视图控制器的push或pop操作相关的任务时,你能够指派一个托付对象给你的导航控制器。这个托付对象必须遵守协议。
Navigation Controller Views
A navigation controller is a container view controller—that is, it embeds the content of other view controllers inside of itself. You access a navigation controller’s view from its
property. This view incorporates the navigation bar, an optional toolbar, and the content view corresponding to the topmost view controller. 导航控制器是一个容器视图控制器——也就是说,它内部嵌入了其它视图控制器的内容。
你能够通过属性訪问导航控制器的view。这个view包含一个navigation bar、一个toolbar、和最顶层视图控制器的内容视图。
The navigation controller manages the creation, configuration, and display of the navigation bar and optional navigation toolbar. It is permissible to customize the navigation bar’s appearance-related properties but you must never change its
,
, or
values directly. If you subclass UINavigationBar
, you must initialize your navigation controller using the
method. To hide or show the navigation bar, use the
property or
method. 导航控制器管理着 navigation bar和一个可选的 toolbar的创建、配置和显示。
它同意开发人员定制navigation bar显示相关的属性,可是你不能直接去改动它的,
或
值。假设你子类化了
UINavigationBar
。你必须使用方法初始化你的导航控制器。显示或隐藏navigation bar,能够设置其
属性。或使用
方法。
A navigation controller builds the contents of the navigation bar dynamically using the navigation item objects (instances of the
class) associated with the view controllers on the navigation stack. To change the contents of the navigation bar, you must therefore configure the navigation items of your custom view controllers. For more information about navigation items, see . 导航控制器使用跟导航栈上的视图控制器相关联的 navigation item对象(
类的实例)动态的构建 navigation bar的内容,因此想要改变 navigation bar的内容,你必须配置你自己定制的视图控制器的 navigation items。关于 navigation items的很多其它内容,请阅读
Updating the Navigation Bar
When the user changes the top-level view controller, the navigation controller updates the navigation bar accordingly. Specifically, the navigation controller updates the bar button items displayed in each of the three navigation bar positions: left, middle, and right. Bar button items are instances of the
class. You can create items with custom content or create standard system items depending on your needs. For more information about how to create bar button items, see . 当用户改变顶层视图控制器,导航控制器会对应更新 navigation bar。也就是。导航控制器会更新显示在 navigation bar左、中、右三个位置的 button items。 Bar button items是
类的实例。 你能够依据你的须要用自己定义的内容创建items。也能够创建系统标准的items。很多其它关于怎样创建button items的内容,请阅读UIBarButtonItem Class Reference。
For all but the root view controller on the navigation stack, the item on the left side of the navigation bar provides navigation back to the previous view controller. The contents of this left-most button are determined as follows:
除了根视图控制器以外的其它全部视图控制器, navigation bar左側的 item提供一个用于返回前一个视图控制器的导航button。
这个最左側的button的内容由下面规则决定:
- If the new top-level view controller has a custom left bar button item, that item is displayed. To specify a custom left bar button item, set the
property of the view controller’s navigation item.
- 假设新的顶层视图控制器有一个定制的 left bar button item。那么这个item会显示。
能够通过设置新的顶层视图控制器navigation item的
属性指定一个定制的left bar button item。
- If the top-level view controller does not have a custom left bar button item, but the navigation item of the previous view controller has an object in its
property, the navigation bar displays that item.
- 假设顶层视图控制器没有定制的bar button item,可是前一个视图控制器navigation item的
属性里面有一个对象,navigation bar将会显示这个item。
- If a custom bar button item is not specified by either of the view controllers, a default back button is used and its title is set to the value of the
property of the previous view controller—that is, the view controller one level down on the stack. (If there is only one view controller on the navigation stack, no back button is displayed.)
- 假设视图控制器都没有指定定制的bar button item,那么将会显示一个默认的返回button。它的标题会被设置为前一个视图控制器的
属性的值,前一个视图控制器也就是在导航栈中比当前视图控制器低一个层级的视图控制器。
(假设仅有一个视图控制器在导航栈里面。那么返回button将不会显示。)
NOTE
In cases where the title of a back button is too long to fit in the available space, the navigation bar may substitute the string “Back” for the actual button title. The navigation bar does this only if the back button is provided by the previous view controller. If the new top-level view controller has a custom left bar button item—an object in the
or
property of its navigation item—the navigation bar does not change the button title. 假设返回button的标题太长,navigation bar 可能会用“back”替换掉实际的button标题。这个替换仅仅发生在返回button由前一个视图控制器提供时。假设新的顶层视图控制器有一个定制的 bar button item—— navigation item的
和
The navigation controller updates the middle of the navigation bar as follows:
导航视图控制器遵从例如以下规则更新 navigation bar的中间部分:
- If the new top-level view controller has a custom title view, the navigation bar displays that view in place of the default title view. To specify a custom title view, set the
property of the view controller’s navigation item.
- 假设新的顶层视图控制器有一个定制的title view,navigation bar会显示这个定制的title view替换掉默认的title view。能够通过设置视图控制器navigation item的
属性指定一个定制的title view。
- If no custom title view is set, the navigation bar displays a label containing the view controller’s default title. The string for this label is usually obtained from the
property of the view controller itself. If you want to display a different title than the one associated with the view controller, set the
title
property of the view controller’s navigation item instead.
- 假设没有设置定制的title view,navigation bar就显示包括视图控制器默认标题的一个标签。这个标签的字符串一般是从视图控制器自己的
属性获得的。假设你想显示一个与视图控制器相关联的标题不同样的标题。你能够设置视图控制器navigation item的
title
属性。
The navigation controller updates the right side of the navigation bar as follows:
导航视图控制器遵从例如以下规则更新 navigation bar的右边部分:
- If the new top-level view controller has a custom right bar button item, that item is displayed. To specify a custom right bar button item, set the
property of the view controller’s navigation item.
- 假设新的顶层视图控制有一个定制的right bar button item。这个item将会被显示。能够通过设置视图控制器navigation item的
属性指定一个定制的right bar button item。
- If no custom right bar button item is specified, the navigation bar displays nothing on the right side of the bar.
- 假设没有定制的right bar button item,navigation bar的右側就什么都不会显示。
The navigation controller updates the navigation bar each time the top view controller changes. Thus, these changes occur each time a view controller is pushed onto the stack or popped from it. When you animate a push or pop operation, the navigation controller similarly animates the change to the navigation bar content.
导航控制器会在每次更新顶层视图控制器的时候更新 navigation bar,也就是说,当视图控制器从导航栈push或pop时。这些变化都会发生。当你以动画一个push或pop操作。导航控制器也会相同的对 navigation bar的内容动画这些变化。
Tinting of the navigation bar is controlled by properties of the navigation bar itself. Use the
property to change the tint color of items in the bar and use the
property to change the tint color of the bar itself. Navigation bars do not inherit their tint color from the currently displayed view controller. navigation bar的着色被它自己的属性控制。能够使用
属性改变 navigation bar中 items的 tint color,使用
属性改变 navigation bar自身的 tint color。 navigation bar 不会从当前显示的视图控制器来继承 tint color 。 For more information about the navigation bar, see .
很多其它关于 navigation bar的很多其它信息,请阅读
Displaying a Navigation Toolbar
A navigation controller object manages an optional toolbar in its view hierarchy. When displayed, this toolbar obtains its current set of items from the
property of the active view controller. When the active view controller changes, the navigation controller updates the toolbar items to match the new view controller, animating the new items into position when appropriate. 导航控制器在它的视图层级中管理着一个可选的 toolbar。当这个 toolbar显示的时候,它从活动的视图控制器的
属性获取它当前的 items的集合。 当活动的视图控制器发生改变。导航控制器就会更新toolbar的items以匹配新的视图控制器,并动画新的items到合适的位置。
The navigation toolbar is hidden by default but you can show it for your navigation interface by calling the
method of your navigation controller object. If not all of your view controllers support toolbar items, your delegate object can call this method to toggle the visibility of the toolbar during subsequent push and pop operations. To use a custom UIToolbar
subclass, use the
method to initialize the navigation controller. 导航控制器的 toolbar默认情况下是隐藏的,但你能够通过调用导航控制器的
方法将它设置为显示。假设你并非全部的视图控制器都支持 toolbar items,你能够在随后的push或pop操作时。用托付对象发送这个消息来让 toolbar隐藏或显示。 假设要使用一个自己定义的UIToolbar
子类。须要使用 方法初始化导航控制器。
State Preservation
In iOS 6 and later, if you assign a value to this view controller’s
property, it attempts to preserve the child view controllers on its navigation stack. The navigation controller starts at the bottom of the stack and moves upward, encoding each view controller that also has a valid restoration identifier string. During the next launch cycle, the navigation controller restores the preserved view controllers to the navigation stack in the same order that they were preserved. iOS 6及之后的版本号,假设你给视图控制器的
属性指定一个值,它会试图去保存它所在的导航栈里面的子视图控制器。导航控制器从栈底開始向上移动。编码每个有有效恢复标识字符串的视图控制器。在下一次的启动循环中。导航控制器依照之前保存的顺序恢复视图控制器到导航栈中。 The child view controllers you push onto the navigation stack may use the same restoration identifiers. The navigation controller automatically stores additional information to ensure that each child’s restoration path is unique.
被push到导航栈的子视图控制器可能使用同样的恢复标识符。导航控制器会自己主动保存额外的信息确保每个子恢复路径都是唯一的。
For more information about how state preservation and restoration works, see .
很多其它关于状态保存和恢复的内容。请阅读 App Programming Guide for iOS。