Navigation Controller

ajith_karuna

AJITHKUMAR K

Posted on January 2, 2023

Navigation Controller

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

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)
    }

Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

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]  
Enter fullscreen mode Exit fullscreen mode

sample App with navigationControl

Image description

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

💖 💪 🙅 🚩
ajith_karuna
AJITHKUMAR K

Posted on January 2, 2023

Join Our Newsletter. No Spam, Only the good stuff.

Sign up to receive the latest update from our blog.

Related