Navigation Controller
We have covered segues for view controller navigation in this tutorial. Nevertheless, the segue is not a useful technique to move between two view controllers because it constantly keeps a new view controller open while going backward rather than simply bringing that view controller out.
We will talk about the superior navigational strategy in this instructional segment. The container view controller that keeps track of a stack of view controllers for traversing hierarchical content is known as the navigation controller. It is an example of the UIViewController-derived UINavigationController class.
class UINavigationController : UIViewController
In the navigation interface, one or more child view controllers are under the management of the Navigation Controller. You may find a navigation controller in practically every iOS application. A single view controller is displayed on screen at a time, even while one or more child view controllers are managed into the navigation stack. A new view controller appears on the screen when an item in the view controller is selected. The previous view controller is hidden because this procedure is animated. Let’s examine the iOS settings app’s navigation design.
A navigation bar with the view controller’s title and back button is present in each and every view controller that is integrated into the navigation controller. Tapping the back button removes the top view controller from the navigation stack. Nevertheless, the root view of the stack does not have a back button.
The root view controller, or initial item in the array, is the one at the bottom of the navigation stack and is managed by a navigation controller. The topmost view controller in the array is the last thing on display at this time. Using the UINavigationController class’s methods, we may push or pop View Controllers into the stack.
Moreover, the navigation controller controls the navigation bar, which is always visible at the top of the screen, for each view controller in the navigation stack. Additionally, an optional toolbar at the bottom of the screen is controlled by the navigation controller.
A navigation controller’s behavior is preserved by a delegate object. It has the ability to offer personalized animations for pushing and popping view controllers. It can also indicate which orientation the navigation interface should be in.
A navigation controller is in charge of the objects shown in the following image.
In the navigation stack, a Navigation Controller takes on the perspective of the view controller at the top. The navigation controller’s view attribute allows us to access this view. On the other hand, the view includes the optional toolbar, the content view, and the navigation bar. The views remain the same, but the content of the toolbar and menu bar is dynamic.
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
This example demonstrates how to push a view controller onto the navigation stack and how to embed a view controller within a navigation controller.
Interface Builder
When we create a new project, we will receive the initial view controller that is depicted in the following image.
Go to Editor -> Embed in and select Navigation Controller, as seen in the accompanying figure, to embed this view controller in the navigation stack.
As a result, a navigation bar will appear at the top of the navigation controller. It is important to note that the first view controller in this instance is the navigation controller.
In order to put another view controller into the navigation stack, let’s add another view controller to the application and set its navigation bar title. This ViewController has a storyboard id of SecondVC defined.
Here, we’ve added a button (display) that, when tapped, pushes the Second View Controller.
ViewController.swift
import UIKit
class ViewController: UIViewController {
let sb = UIStoryboard(name: "Main", bundle: nil)
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
}
@IBAction func clickedButtonShow(_ sender: Any) {
let secondVC = sb.instantiateViewController(identifier: "SecondVC")
self.navigationController?.pushViewController(secondVC, animated: true)
}
}