8

How to Hide Toolbar on Scroll in iOS

 1 year ago
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.
neoserver,ios ssh client

By default, a toolbar in UINavigationController will always be visible at the bottom of the screen.

A toolbar in iOS.

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

hidesBarsOnSwipe = true

You can easily support sarunw.com by checking out this sponsor.

Turn your code into a snapshot:

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

About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK