How to Hide Navigation Bar on Scroll in UIKit
Table of Contents
By default, a navigation bar in UINavigationController
will always be visible at the top of the screen.
You might not be aware of this, but since iOS 8, UINavigationController
has a property that can hide a navigation bar when users scroll through the content.
Why do we need to Hide a navigation bar on scrolling
This may benefit your app if you want users to focus on your content and get the most available screen out of it.
Here is how it looks.
You can easily support sarunw.com by checking out this sponsor.
AI Grammar: Correct grammar, spell check, check punctuation, and parphrase.
How to Hide Navigation Bar on Scroll in UIKit
To make a navigation bar show and hide according to the scrolling, we set the hidesBarsOnSwipe
property to true
.
Setting this value to true
will enable these two behaviors.
- Hides the navigation bar when users scroll down.
- When users try to go back up, the navigation bar will reappear.
You set this value on a UINavigationController
instance.
let nav = UINavigationController(rootViewController: MyViewController())
nav.hidesBarsOnSwipe = true
You can also set this value from a view controller.
override func viewDidLoad() {
super.viewDidLoad()
navigationController?.hidesBarsOnSwipe = true
}
Since it is a navigation controller's property, setting it to true
will enable the behavior on any view controller under the navigation controller.
So, make sure you set it back to false
if you want to enable this behavior only on a specific view.
navigationController?.hidesBarsOnSwipe = false
Read more article about UIKit, UINavigationBar, or see all available topic
Enjoy the read?
If you enjoy this article, you can subscribe to the weekly newsletter.
Every Friday, you'll get a quick recap of all articles and tips posted on this site. No strings attached. Unsubscribe anytime.
Feel free to follow me on Twitter and ask your questions related to this post. Thanks for reading and see you next time.
If you enjoy my writing, please check out my Patreon https://www.patreon.com/sarunw and become my supporter. Sharing the article is also greatly appreciated.
Become a patron Buy me a coffee Tweet ShareAllow users to manage In-App Subscription in SwiftUI
In iOS 15, we finally got a way for users to manage their subscriptions right within the app.
How to Run code in Release build in Xcode
Learn how to run it on the Release build configuration, which is the one Xcode used for the App Store.