A container view controller that defines a stack-based scheme for navigating hierarchical content.
- iOS 2.0+
- Mac Catalyst 13.0+
- tvOS 9.0+
A navigation controller is a container view controller that manages one or more child view controllers in a navigation interface. In this type of interface, only one child view controller is visible at a time. Selecting an item in the view controller pushes a new view controller onscreen using an animation, thereby hiding the previous view controller. Tapping the back button in the navigation bar at the top of the interface removes the top view controller, thereby revealing the view controller underneath.
Use a navigation interface to mimic the organization of hierarchical data managed by your app. 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.
A navigation controller object manages its child view controllers using an ordered array, known as the navigation stack. The first view controller in the array is the root view controller and represents the bottom of the stack. The last view controller in the array is the topmost item on the stack, and represents 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 its child view controllers. When the
is 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 view controllers, provide custom animation transitions, and specify the preferred orientation for the navigation interface. The delegate object you provide must conform to the
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.
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.
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
alpha values directly. If you subclass
UINavigation, you must initialize your navigation controller using the
init(navigation method. To hide or show the navigation bar, use the
is property or
A navigation controller builds the contents of the navigation bar dynamically using the navigation item objects (instances of the
UINavigation 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
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
UIBar 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
tint property to change the tint color of items in the bar and use the
bar property to change the tint color of the bar itself. Navigation bars do not inherit their tint color from the currently displayed view controller.
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
leftproperty of the view controller’s navigation item.
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
backproperty, the navigation bar displays that item.
Bar Button 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
titleproperty 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
titleproperty 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
titleproperty of the view controller itself. If you want to display a different title than the one associated with the view controller, set the
titleproperty 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
rightproperty of the view controller’s navigation item.
Bar Button 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
toolbar 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
set 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(navigation 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.
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
Controller Supported Interface Orientations(_:)
Presentation context—A navigation controller defines the presentation context for modally presented view controllers. When the modal transition style is
Presentation Style .current Context
UIModal, modal presentations from the view controllers in the navigation stack cover the entire navigation interface.
Presentation Style .over Current Context
When you assign a value to a navigation controller’s
restoration property, it attempts to preserve itself and 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 Preserving Your App's UI Across Launches.