How to hide Back Button on navigation bar on iPhone/iPad?

iOSMobile DevelopmentiPhone/iPad

To hide the back button on navigation bar we’ll have to either set the navigation button as nil and then hide it or hide it directly.

Let’s create a project, add 2 view controller and Embed them in navigation controller. Let’s see how this project looks when run without any code to remove the navigation bar.

This code set’s the navigation bar’s back button as hidden.

self.navigationController?.navigationItem.hidesBackButton = true

This code set’s the navigation bar’s back button as nil

self.navigationItem.leftBarButtonItem = nil;

A combination of these to approaches would be a better solution and works even if you have set a custom navigation bar.

self.navigationItem.leftBarButtonItem = nil
self.navigationItem.hidesBackButton = true

You can also use

override func viewDidLoad() {
   super.viewDidLoad()
   self.navigationItem.setHidesBackButton(true, animated: false)
}

Let’s see how the app looks after adding the above code to new view controller.

Note − Do not forget to assign class to the Second View controller and add the above code in that class.

raja
Published on 28-Dec-2018 17:07:13
Advertisements