Certain screens in an app look better when the navigation bar is hidden.
To hide the navigation bar in Swift, you’ll need to add code to two methods: viewWillAppear
and viewWillDisappear
.
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
navigationController?.setNavigationBarHidden(true, animated: animated)
}
override func viewWillDisappear(_ animated: Bool) {
super.viewWillDisappear(animated)
navigationController?.setNavigationBarHidden(false, animated: animated)
}
That’s it to hide the navigation bar in your view controller.
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
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
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
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
The Complete iOS App Development Bootcamp
Disclosure: This website may contain affiliate links, meaning when you click the links and make a purchase, we receive a commission.