Class

UINavigationController

The UINavigationController class implements a specialized view controller that manages the navigation of hierarchical content. This navigation interface makes it possible to present your data efficiently and makes it easier for the user to navigate that content. You generally use this class as-is but you may also subclass to customize the class behavior.

Overview

The screens presented by a navigation interface typically mimic the hierarchical organization of your data. At each level of the hierarchy, you provide an appropriate screen (managed by a custom view controller) to display the content at that level. Figure 1 shows an example of the navigation interface presented by the Settings application in iOS Simulator. The first screen presents the user with the list of applications that contain preferences. Selecting an application reveals individual settings and groups of settings for that application. Selecting a group yields more settings and so on. For all but the root view, the navigation controller provides a back button to allow the user to move back up the hierarchy.

Figure 1

A sample navigation interface

A sample navigation interface

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 is the root view controller. The last view controller in the array is the view controller currently being displayed. You add and remove view controllers from the stack using segues or using the methods of this class. The user can also remove the topmost view controller using the back button in the navigation bar or using a left-edge swipe gesture.

The navigation controller manages the navigation bar at the top of the interface and an optional toolbar at the bottom of the interface. The navigation bar is always present and is managed by the navigation controller itself, which updates the navigation bar using the content provided by the view controllers on the navigation stack. When the isToolbarHidden property is false, the navigation controller similarly updates the toolbar with contents provided by the topmost view controller.

A navigation controller coordinates its behavior with its delegate object. The delegate object can override the pushing or popping of a view controller, provide custom animation transitions, and specify the preferred orientation for the navigation interface. The delegate object you provide must conform to the UINavigationControllerDelegate protocol.

Figure 2 shows the relationships between the navigation controller and the objects it manages. Use the specified properties of the navigation controller to access these objects.

Figure 2

Objects managed by the navigation controller

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. Figure 3 shows how these views are assembled to present the overall navigation interface. (In this figure, the navigation interface is further embedded inside a tab bar interface.) Although the content of the navigation bar and toolbar views changes, the views themselves do not. The only view that actually changes is the custom content view provided by the topmost view controller on the navigation stack.

Figure 3

The views of a navigation controller

The views of a navigation controller

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 frame, bounds, or alpha values directly. If you subclass UINavigationBar, you must initialize your navigation controller using the init(navigationBarClass:toolbarClass:) method. To hide or show the navigation bar, use the isNavigationBarHidden property or setNavigationBarHidden(_:animated:) method.

A navigation controller builds the contents of the navigation bar dynamically using the navigation item objects (instances of the UINavigationItem class) associated with the view controllers on the navigation stack. To customize the overall appearance of a navigation bar, use UIAppearance APIs. 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.

Updating the Navigation Bar

Each time the top-level view controller changes, 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.

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.

For more information about the navigation bar, see UINavigationBar. For more information about how to create bar button items, see UIBarButtonItem.

The Left Item

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:

  • 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.

  • 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.

  • 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.)

The Middle Item

The navigation controller updates the middle of the navigation bar as follows:

  • 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.

  • 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.

The Right Item

The navigation controller updates the right side of the navigation bar as follows:

  • 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.

  • If no custom right bar button item is specified, the navigation bar displays nothing on the right side of the bar.

Displaying a 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.

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, initialize the navigation controller using the init(navigationBarClass:toolbarClass:) method. If you use custom toolbar and navigation bar subclasses to create a navigation controller, note that you are responsible for pushing and setting view controllers before presenting the navigation controller onscreen.

Adapting to Different Environments

The navigation interface remains the same in both horizontally compact and horizontally regular environments. When toggling between the two environments, only the size of the navigation controller’s view changes. The navigation controller does not change its view hierarchy or the layout of its views.

When configuring segues between view controllers on a navigation stack, the standard Show and Show Detail segues behave as follows:

  • Show segue—The navigation controller pushes the specified view controller onto its navigation stack.

  • Show Detail segue—The navigation controller presents the specified view controller modally.

The behaviors of other segue types are unchanged.

Interface Behaviors

A navigation controller supports the following behaviors for its interface:

  • Supported interface orientations—A navigation controller object does not consult the view controllers on its navigation stack when determining the supported interface orientations. On iPhone, a navigation controller supports all orientations except portrait upside-down. On iPad, a navigation controller supports all orientations. If the navigation controller has a delegate object, the delegate can specify a different set of supported orientations using the navigationControllerSupportedInterfaceOrientations(_:) method.

  • Presentation context—A navigation controller defines the presentation context for modally presented view controllers. When the modal transition style is currentContext or overCurrentContext, modal presentations from the view controllers in the navigation stack cover the entire navigation interface.

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.

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.

For more information about how state preservation and restoration works, see App Programming Guide for iOS.

Symbols

Creating Navigation Controllers

init(rootViewController: UIViewController)

Initializes and returns a newly created navigation controller.

init(navigationBarClass: AnyClass?, toolbarClass: AnyClass?)

Initializes and returns a newly created navigation controller that uses your custom bar subclasses.

Accessing Items on the Navigation Stack

var topViewController: UIViewController?

The view controller at the top of the navigation stack.

var visibleViewController: UIViewController?

The view controller associated with the currently visible view in the navigation interface.

var viewControllers: [UIViewController]

The view controllers currently on the navigation stack.

func setViewControllers([UIViewController], animated: Bool)

Replaces the view controllers currently managed by the navigation controller with the specified items.

Pushing and Popping Stack Items

func pushViewController(UIViewController, animated: Bool)

Pushes a view controller onto the receiver’s stack and updates the display.

func popViewController(animated: Bool)

Pops the top view controller from the navigation stack and updates the display.

func popToRootViewController(animated: Bool)

Pops all the view controllers on the stack except the root view controller and updates the display.

func popToViewController(UIViewController, animated: Bool)

Pops view controllers until the specified view controller is at the top of the navigation stack.

var interactivePopGestureRecognizer: UIGestureRecognizer?

The gesture recognizer responsible for popping the top view controller off the navigation stack.

Configuring Navigation Bars

var navigationBar: UINavigationBar

The navigation bar managed by the navigation controller.

func setNavigationBarHidden(Bool, animated: Bool)

Sets whether the navigation bar is hidden.

Configuring Custom Toolbars

var toolbar: UIToolbar!

The custom toolbar associated with the navigation controller.

func setToolbarHidden(Bool, animated: Bool)

Changes the visibility of the navigation controller’s built-in toolbar.

var isToolbarHidden: Bool

A Boolean indicating whether the navigation controller’s built-in toolbar is visible.

Hiding the Navigation Bar

var hidesBarsOnTap: Bool

A Boolean value indicating whether the navigation controller allows hiding of its bars using a tap gesture.

var hidesBarsOnSwipe: Bool

A Boolean value indicating whether the navigation bar hides its bars in response to a swipe gesture.

var hidesBarsWhenVerticallyCompact: Bool

A Boolean value indicating whether the navigation controller hides its bars in a vertically compact environment.

var hidesBarsWhenKeyboardAppears: Bool

A Boolean value indicating whether the navigation controller hides its bars when the keyboard appears.

var isNavigationBarHidden: Bool

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

var barHideOnTapGestureRecognizer: UITapGestureRecognizer

The gesture recognizer used to hide and show the navigation and toolbar.

var barHideOnSwipeGestureRecognizer: UIPanGestureRecognizer

The gesture recognizer used to hide the navigation bar and toolbar.

Accessing the Delegate

var delegate: UINavigationControllerDelegate?

The delegate of the navigation controller object.

Action Method for Displaying View Controllers

func show(UIViewController, sender: Any?)

Presents the specified view controller in the navigation interface.

Constants

UINavigationControllerHideShowBarDuration

A global constant that specifies a preferred duration when animating the navigation bar.