Javatpoint Logo
Javatpoint Logo

Navigation Controller

In this tutorial, we have discussed segues for the navigation between view controllers. However, to navigate between two view controllers, the segue is not a good option as it always maintains a new view controller while navigating backward instead of just popping out that view controller.

In this section of the tutorial, we will discuss the better approach for navigation. The Navigation Controller can be defined as the container view controller that maintains a stack of View Controllers for navigating hierarchical content. It is an instance of the UINavigationController class, which inherits UIViewController.

The Navigation Controller manages one or more child view controllers in the navigation interface. Navigation Controllers are being used in almost every iOS application. Even though one or more child view controllers are managed into navigation stack, only one view controller appears on-screen at an instance. Selecting an item in the view controller pushes a new view controller on the screen. This process is animated and therefore hides the previous view controller. Let's look at the navigation interface used in the settings app on iOS.

Navigation Controller Navigation Controller

All the View Controllers embedded in the Navigation Controller contain a navigation bar that contains the title of the view controller and back button. The top view controller is removed from the navigation stack by tapping the back button. However, the back button is not provided for the root view of the stack.

A Navigation Controller manages the View Controllers in the ordered array where the first item is considered as the root view controller and also the bottom of the navigation stack. The last item in the array is the topmost view controller, which is currently being displayed. We can push or pop View Controllers into the stack using the methods of UINavigationController class.

The navigation controller also manages the navigation bar for all the view controllers of the navigation stack, which always appears on the top of the screen. The navigation controller also manages an optional toolbar on the bottom of the screen.

A delegate object maintains the behavior of a navigation controller. It can provide the custom animations to pushing and popping the view controllers. It can also specify the preferred orientation for the navigation interface.

The Following image shows the objects managed by a navigation controller.

Navigation Controller

A Navigation Controller adopts the view of the topmost view controller in the navigation stack. We can access this view by using the view property of the navigation controller. However, the view contains the navigation bar, the content view, and the optional toolbar. The content of the navigation bar and the toolbar changes, whereas the views do not change.

Navigation Controller properties

SN Property Description
1 var delegate: UINavigationControllerDelegate? It represents the delegate object for the navigation controller.
2 protocol UINavigationControllerDelegate The View Controller must conform to this protocol while using the delegate object to modify behavior when the view controller is pushed or popped from the navigation stack.
3 var topViewController: UIViewController? It represents the topmost view controller in the navigation stack.
4 var visibleViewController: UIViewController? It represents the view controller that is associated with the currently visible view of the navigation stack.
5 var viewControllers: [UIViewController] It represents the array of UIViewController that is part of the navigation stack.
6 var interactivePopGestureRecognizer: UIGestureRecognizer? It is the gesture recognizer used to remove the topmost view controller off the navigation stack.
7 var navigationBar: UINavigationBar It represents the navigation bar associated with the Navigation Controller.
8 var toolbar: UIToolbar! It represents the ToolBar object associated with the navigation controller.
9 var isToolbarHidden: Bool It is a Boolean value indicating whether the toolbar is hidden with the navigation controller or not.
10 var hidesBarsOnTap: Bool It represents a Boolean value indicating whether the navigation controller allows hiding of its bars using a tap gesture.
11 var hidesBarsOnSwipe: Bool It represents a Boolean value indicating whether the navigation bar hides its bars in response to a swipe gesture.
12 var hidesBarsWhenVerticallyCompact: Bool It represents a Boolean value indicating whether the navigation controller hides its bars in a vertically compact environment.
13 var hidesBarsWhenKeyboardAppears: Bool It represents a Boolean value indicating whether the navigation controller hides its bars when the keyboard appears.
14 var isNavigationBarHidden: Bool It represents a boolean value indicating whether the navigation bar is hidden or not.
15 var barHideOnTapGestureRecognizer: UITapGestureRecognizer It represents the gesture recognizer used to hide and show the navigation and toolbar.
16 var barHideOnSwipeGestureRecognizer: UIPanGestureRecognizer It represents the gesture recognizer used to hide the navigation bar and toolbar.

Navigation Controller Methods

SN Method Description
1 init(rootViewController: UIViewController) It returns a newly created navigation controller with the specified root view controller.
2 init(navigationBarClass: AnyClass?, toolbarClass: AnyClass?) t initializes and returns a newly created navigation controller having the specified class for the navigation bar.
5 func setViewControllers([UIViewController], animated: Bool) It is used to replace the view controllers that are currently managed by the navigation stack.
6 func pushViewController(UIViewController, animated: Bool) This method is used to push the specified view controller onto the receiver's stack and update the display.
7 func popViewController(animated: Bool) -> UIViewController? It returns a view controller that is popped (removed) from the navigation controller. This method is automatically triggered when the user taps the back button in the navigation bar.
8 func popToRootViewController(animated: Bool) -> [UIViewController]? It pops all the view controllers on the stack except the root view controller and update then display.
9 func popToViewController(UIViewController, animated: Bool) -> [UIViewController]? This method is used to pop to the specified view controller in the navigation stack.
10 func setNavigationBarHidden(Bool, animated: Bool) This method is used to hide the navigation bar associated with the navigation controller.
11 func setToolbarHidden(Bool, animated: Bool) This method is used to hide the toolbar associated with the navigation controller.
12 func show(UIViewController, sender: Any?) This method is used to show a specified view controller.

Example

In this example, we will see how to embed the view controller into a navigation controller and how can we push a view controller onto the navigation stack.

Interface Builder

We will get an initial view controller shown in the following image on creating a new project.

Navigation Controller

To embed this view controller in the navigation stack, go to Editor -> Embed in and choose Navigation Controller as shown in the following image.

Navigation Controller

This will create a navigation controller having a navigation bar at the top of it. Here, we must notice that the navigation controller is the initial view controller.

Navigation Controller

Here we define the navigation bar title for the view controller, and Let's add another view controller in the application so that we can push that view controller in the navigation stack. Define a storyboard id as SecondVC for this ViewController.

Navigation Controller

Here, we have added a button (show) by tapping on which the Second View Controller is pushed.

Navigation Controller

ViewController.swift

Output

Navigation Controller
Next TopicNavigation Bar




Youtube For Videos Join Our Youtube Channel: Join Now

Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Preparation


Trending Technologies


B.Tech / MCA