How to Hide Toolbar on Scroll in iOS
source link: https://sarunw.com/posts/hide-toolbar-on-scroll-in-uikit/
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
By default, a toolbar in UINavigationController
will always be visible at the bottom of the screen.
A toolbar in iOS.
Since iOS 8, UINavigationController
expose a property, hidesBarsOnSwipe
, that can hide a toolbar when users scrolling.
Why do we need to Hide a toolbar on scrolling
You probably need to hide a toolbar if you want to utilize screen resources as much as possible.
This might suit an app that main focus is to consume content.
Here is how it looks in action.
hidesBarsOnSwipe = true
You can easily support sarunw.com by checking out this sponsor.
Sponsor sarunw.com and reach thousands of iOS developers.
How to Hide Toolbar on Scroll in UIKit
To make a toolbar 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 toolbar when users scroll down.
- When users try to go back up, the toolbar will reappear.
hidesBarsOnSwipe
also shows and hides a navigation bar along with a toolbar.
You set this value on a UINavigationController
instance.
let nav = UINavigationController(rootViewController: MyViewController())
nav.hidesBarsOnSwipe = true
You can also set this value from within a view controller by reference to navigationController
.
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 on a specific view.
navigationController?.hidesBarsOnSwipe = false
Recommend
About Joyk
Aggregate valuable and interesting links.
Joyk means Joy of geeK