码迷,mamicode.com
首页 > 其他好文 > 详细

UINavigationController

时间:2015-06-08 23:29:07      阅读:282      评论:0      收藏:0      [点我收藏+]

标签:ios开发   移动互联网   objective-c   视图控制器   

本文摘录了官方文档中关于导航控制器的部分重要信息进行了翻译,便于查阅。

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 pushViewController:animated: method.Pushing a view controller displays its view in the navigation interface and updates the navigation controls accordingly. 

通常,添加一个视图控制器到栈顶使用:pushViewController:animated: 方法,当push一个视图控制器时,视图控制器会将它自己的视图显示到导航界面并更新相应的导航控制。

To remove a view controller from the stack programmatically, you call the popViewControllerAnimated: method. 

使用编程方式从导航栈中移除一个视图控制器,你可以调用:popViewControllerAnimated: 方法。

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 UINavigationControllerDelegate protocol.

当活动的视图控制器发生变化时,导航控制会把这些变化通知给它的委托对象。如果你想执行一些附加的设置或者清理一些与视图控制器的push或pop操作相关的任务时,你可以指派一个委托对象给你的导航控制器。这个委托对象必须遵守UINavigationControllerDelegate协议。

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 view property. This view incorporates the navigation bar, an optional toolbar, and the content view corresponding to the topmost view controller. 

导航控制器是一个容器视图控制器——也就是说,它内部嵌入了其他视图控制器的内容。你可以通过view属性访问导航控制器的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 framebounds, or alpha values directly. If you subclass UINavigationBar, you must initialize your navigation controller using the initWithNavigationBarClass:toolbarClass: method. To hide or show the navigation bar, use the navigationBarHidden property or setNavigationBarHidden:animated: method.

导航控制器管理着navigation bar和一个可选的toolbar的创建、配置和显示。它允许开发者定制navigation bar显示相关的属性,但是你不能直接去修改它的frameboundsalpha值。如果你子类化了UINavigationBar,你必须使用initWithNavigationBarClass:toolbarClass:方法初始化你的导航控制器。显示或隐藏navigation bar,可以设置其navigationBarHidden属性,或使用setNavigationBarHidden:animated: 方法。

A navigation controller builds the contents of the navigation bar dynamically using the navigation item objects (instances of the UINavigationItemclass) 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 UINavigationItem Class Reference.

导航控制器使用跟导航栈上的视图控制器相关联的navigation item对象(UINavigationItem类的实例)动态的构建navigation bar的内容,因此想要改变navigation bar的内容,你必须配置你自己定制的视图控制器的navigation items。关于
navigation items的更多内容,请阅读UINavigationItem Class Reference。

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 UIBarButtonItem 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 UIBarButtonItem Class Reference.

当用户改变顶层视图控制器,导航控制器会相应更新navigation bar。也就是,导航控制器会更新显示在navigation bar左、中、右三个位置的button itemsBar button itemsUIBarButtonItem类的实例。你可以根据你的需要用自定义的内容创建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提供一个用于返回前一个视图控制器的导航按钮。这个最左侧的按钮的内容由以下规则决定:

  • 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 leftBarButtonItem property of the view controller’s navigation item.

  • 如果新的顶层视图控制器有一个定制的 left bar button item,那么这个item会显示。可以通过设置新的顶层视图控制器navigation item的leftBarButtonItem属性指定一个定制的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 backBarButtonItem property, the navigation bar displays that item.

  • 如果顶层视图控制器没有定制的bar button item,但是前一个视图控制器navigation item的backBarButtonItem属性里面有一个对象,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 title 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,那么将会显示一个默认的返回按钮,它的标题会被设置为前一个视图控制器的title属性的值,前一个视图控制器也就是在导航栈中比当前视图控制器低一个层级的视图控制器。(如果仅有一个视图控制器在导航栈里面,那么返回按钮将不会显示。)


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 leftBarButtonItem or leftBarButtonItemsproperty of its navigation item—the navigation bar does not change the button title.
如果返回按钮的标题太长,navigation bar可能会用“back”替换掉实际的按钮标题。这个替换只发生在返回按钮由前一个视图控制器提供时。如果新的顶层视图控制器有一个定制的bar button item——navigation item的leftBarButtonItem 和 leftBarButtonItems属性有对象,navigation bar不会修改按钮标题。

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 titleView property of the view controller’s navigation item.

  • 如果新的顶层视图控制器有一个定制的title view,navigation bar会显示这个定制的title view替换掉默认的title view。可以通过设置视图控制器navigation item的titleView属性指定一个定制的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 title 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就显示包含视图控制器默认标题的一个标签。这个标签的字符串通常是从视图控制器自己的title属性获得的。如果你想显示一个与视图控制器相关联的标题不相同的标题,你可以设置视图控制器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 rightBarButtonItem property of the view controller’s navigation item.

  • 如果新的顶层视图控制有一个定制的right bar button item,这个item将会被显示。可以通过设置视图控制器navigation item的rightBarButtonItem属性指定一个定制的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 tintColor property to change the tint color of items in the bar and use the barTintColor 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的着色被它自己的属性控制。可以使用tintColor属性改变navigation baritemstint color,使用barTintColor属性改变navigation bar自身的tint color。navigation bar不会从当前显示的视图控制器来继承tint color

For more information about the navigation bar, see UINavigationBar Class Reference.

更多关于navigation bar的更多信息,请阅读UINavigationBar Class Reference。

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 toolbarItems 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显示的时候,它从活动的视图控制器的toolbarItems属性获取它当前的items的集合。当活动的视图控制器发生改变,导航控制器就会更新toolbaritems以匹配新的视图控制器,并动画新的items到合适的位置。

The navigation toolbar is hidden by default but you can show it for your navigation interface by calling the setToolbarHidden:animated: 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 initWithNavigationBarClass:toolbarClass: method to initialize the navigation controller.

导航控制器的toolbar默认情况下是隐藏的,但你可以通过调用导航控制器的setToolbarHidden:animated:方法将它设置为显示。如果你并不是所有的视图控制器都支持toolbar items,你可以在随后的push或pop操作时,用委托对象发送这个消息来让toolbar隐藏或显示。如果要使用一个自定义的UIToolbar子类,需要使用initWithNavigationBarClass:toolbarClass: 方法初始化导航控制器。

State Preservation

In iOS 6 and later, if you assign a value to this view controller’s restorationIdentifier 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及之后的版本,如果你给视图控制器的restorationIdentifier属性指定一个值,它会试图去保存它所在的导航栈里面的子视图控制器。导航控制器从栈底开始向上移动,编码每一个有有效恢复标识字符串的视图控制器。在下一次的启动循环中,导航控制器按照之前保存的顺序恢复视图控制器到导航栈中。

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.

更多关于状态保存和恢复的内容,请阅读App Programming Guide for iOS


UINavigationController

标签:ios开发   移动互联网   objective-c   视图控制器   

原文地址:http://blog.csdn.net/debolee/article/details/46418201

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!