UINavigationController

本文摘錄了官方文檔中關於導航控制器的部分重要信息進行了翻譯,便於查閱。

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


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章