Hide Navigation Bar In Swift Tutorial

Certain screens in an app look better when the navigation bar is hidden.

How To Hide Navigation Bar In Swift

To hide the navigation bar in Swift, you’ll need to add code to two methods: viewWillAppear and viewWillDisappear.

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

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

That’s it to hide the navigation bar in your view controller.

How To Hide Navigation Bar On Tap In Swift

Starting in iOS 8, you can easily make the navigation bar hide on tap. This allows the user to toggle the hiding and showing of the navigation bar by tapping in the content area.

To do this in Swift set hidesBarOnTap to true.

navigationController?.hidesBarsOnTap = true

How To Hide Navigation Bar When Keyboard Is Shown In Swift

The keyboard takes up a lot of space on the screen when it is being shown. To get back some screen real estate, you can hide the navigation bar when the keyboard is shown.

To do this in Swift simply set hidesBarsWhenKeyboardAppears to true:

navigationController?.hidesBarsWhenKeyboardAppears = true

How To Hide Navigation Bar On Swipe Gesture In Swift

We can also hide the navigation on a swipe gesture in Swift. This allows an upward swipe to hide the navigation bar and a downward swipe to show the navigation bar again.

To do this in Swift, simply set hidesBarsOnSwipe to true:

navigationController?.hidesBarsOnSwipe = true

How To Make Navigation Bar Transparent

Another option to hiding the navigation bar is to make it transparent. Here’s how to do that in Swift:

self.navigationController?.navigationBar.setBackgroundImage(UIImage(), for: .default)
self.navigationController?.navigationBar.shadowImage = UIImage()
self.navigationController?.navigationBar.isTranslucent = true
self.navigationController?.view.backgroundColor = .clear
If you liked this post and want to learn more, check out The Complete iOS Developer Bootcamp. Speed up your learning curve - hundreds of students have already joined. Thanks for reading!

Eddy Chung

I teach iOS development on ZeroToAppStore.com.

Similar Posts