UINavigationController
A container view controller that defines a stack-based scheme for navigating hierarchical content.
Declaration
@MainActor class UINavigationControllerMentioned in
Overview
A navigation controller is a container view controller that manages one or more contained view controllers in a navigation interface. In this type of interface, only one contained view controller is visible at a time. Selecting an item in the view controller pushes a new view controller onscreen using an animation, 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. The following image shows an example of the navigation interface presented by the Settings application in a simulated iOS device. The first screen presents the user with the list of groups that organize preferences. Selecting a group reveals individual settings and groups of settings for that application. For all but the root view, the navigation controller provides a back button to allow the user to move back up the hierarchy.
[Image]
A navigation controller object manages the view controllers it contains 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 that the system is currently displaying. 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 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 contained view controllers. 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 view controllers, provide custom animation transitions, and specify the preferred orientation for the navigation interface. The delegate object you provide must conform to the UINavigationControllerDelegate protocol.
The following image shows the relationships between the navigation controller and the objects it manages. Use the specified properties of the navigation controller to access these objects.
[Image]
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. The following image shows how these views assemble 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 don’t. The only view that actually changes is the custom content view provided by the topmost view controller on the navigation stack.
[Image]
The navigation controller manages the creation, configuration, and display of the navigation bar and optional navigation toolbar. Don’t change the navigation bar’s frame, bounds, or alpha values directly. If you subclass UINavigationBar, 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 change the contents of the navigation bar, 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.
When your navigation bar displays with Liquid Glass, don’t add a background or apply a tint color. Add color to the text or image in a bar button item with tintColor. To add color to the background of a bar button item, set the style to UIBarButtonItem.Style.prominent.
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 doesn’t 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 isn’t specified by either of the view controllers, the system uses a default back button that displays a back image.
If there’s only one view controller on the navigation stack, it doesn’t display a back button.
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 custom right bar button items, it displays those items. To specify a custom right-bar button item or items, set the rightBarButtonItem or rightBarButtonItems property of the view controller’s navigation item.
If the view controller doesn’t have any custom right-bar button items, the navigation bar doesn’t display anything 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’re 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 doesn’t 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 doesn’t 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 UIModalPresentationStyle.currentContext or UIModalPresentationStyle.overCurrentContext, modal presentations from the view controllers in the navigation stack cover the entire navigation interface.
State preservation
When you assign a value to a navigation controller’s restorationIdentifier property, it attempts to preserve itself and the contained 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 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 view controller’s restoration path is unique.
For more information about how state preservation and restoration works, see Preserving your app’s UI across launches.
Topics
Creating a navigation controller
Customizing the navigation interface behavior
Accessing items on the navigation stack
Pushing and popping stack items
pushViewController(_:animated:)popViewController(animated:)popToRootViewController(animated:)popToViewController(_:animated:)interactivePopGestureRecognizerinteractiveContentPopGestureRecognizer
Configuring navigation bars
Configuring custom toolbars
Hiding the navigation bar
hidesBarsOnTaphidesBarsOnSwipehidesBarsWhenVerticallyCompacthidesBarsWhenKeyboardAppearsisNavigationBarHiddenbarHideOnTapGestureRecognizerbarHideOnSwipeGestureRecognizer