Navigation Controller
Published: 2023-01-02 06:52:52

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.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/v91z0jdtd826ywl8jpui.png)

**Navigation Controller Methods**

**init(rootViewController: UIViewController)**
    It returns a newly created navigation controller with the specified root view controller.

**init(navigationBarClass: AnyClass?, toolbarClass: AnyClass?)**
   initializes and returns a newly created navigation controller having the specified class for the navigation bar.

**pushViewController(UIViewController, animated: Bool)**
    This method is used to push the specified view controller onto the receiver's stack and update the display.

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

## Navigation Bar

  The navigation bar is used in the association with the navigation controller that is displayed along the top of the screen. It is the instance of the UINavigationBar class that inherits UIView.

The UINavigationBar object is a bar that is displayed along the top of the View Controller window that is embedded in the Navigation Controller. A Navigation Bar contains the navigation items typically bar button items that are used to navigate within the hierarchy of screens. A typical Navigation Bar contains a back button displayed at the left of the bar, a center View Controller title, and the optional right bar buttons.


**Left Item**
   The Left item in the navigation bar provides the backward navigation to the previous view controller in the navigation stack. However, if the current topmost view controller contains a custom left bar button item, then it will be displayed. The leftBarButtonItem property of the View Controller's navigation item is used to set the custom left bar button item.

```
let back=UIBarButtonItem(title: "< Back", style: .plain, target: self, action: #selector(backAction))
        navigationItem.leftBarButtonItem=back
        navigationController?.navigationBar.backgroundColor = .white


@objc func backAction(){
        dismiss(animated: true, completion: nil)
    }

```

**Right Item**
    To specify the custom Right bar button item, we use the rightBarButtonItem property.we can add System default System icosButtons like( save,add,action,camera)
 

```
let add=UIBarButtonItem(systemItem: .add)
        add.target=self
        add.action=#selector(addClickd)
        navigationItem.rightBarButtonItem=add
```

we can add arrayOfBarButtons 

```
let rightButton1= UIBarButtonItem(barButtonSystemItem: .camera, target: self, action: #selector(clickedCamera))  
let rightButton2 = UIBarButtonItem(barButtonSystemItem: .add, target: self, action: #selector(clickedAdd))  
          
 navigationItem.rightBarButtonItems = [rightButton1,rightButton2]  
```

**sample App with navigationControl**


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/7zylum4z6ik5vshvod3q.png)

**when click the row secondView is displayed by navigationControler then click back button previous View will be displayed secondView is pop from the navigationStack**

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/wlnavokw9guzeuzorcw1.png)