When using UINavigationController in your iOS, iPadOS and macOS app sometimes you need to show/hide the navigation bar on a particular view and here is how to do it without disturbing the navigation bar hierarchy in your Main.storyboard file.

Sometimes you want to hide Navigation Bar from a particular view without manually deleting it from Main.storyboard,

So here I have documented how to do it programmatically instead.

Step 01: Copy the following code to your ViewController’s Swift file’s ‘viewWillAppear’ method for which view you want to hide Navigation Bar on its load on your iPhone, iPad or Mac’s (Catalyst) app screen.

override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(true)
self.navigationController?.setNavigationBarHidden(true, animated: true)
}

Step 02: Now copy the following code to your ViewController’s ‘viewWillDisappear’ method on your same View Controller’s Swift file so it will not hurt other view controllers in the hierarchy when you move away from your original view controller.

override func viewWillDisappear(_ animated: Bool) {
super.viewWillDisappear(true)
self.navigationController?.setNavigationBarHidden(false, animated: true)
}

Hope it helps,

Thanks & Regards
Mandar Apte

Leave a Reply

Your email address will not be published.