Hide status bar while scrolling

jmcastel picture jmcastel · Oct 4, 2014 · Viewed 10.1k times · Source

iOS 8 adds a super new cool feature: hiding the navigation bar when user is scrolling.

This with a single line in viewDidload :

navigationController?.hidesBarsOnSwipe = true

Cool, isn't it?

But now I have a little problem: when the navigation bar is hidden, the status bar is still here and overlaps content, which is ugly.

What should I do to make it hidden when the navigation bar is hidden?

Answer

Yariv Nissim picture Yariv Nissim · Dec 19, 2014

Override the following methods on UIViewController:

extension MyViewController {
  override func prefersStatusBarHidden() -> Bool {
    return barsHidden // this is a custom property
  }

  // Override only if you want a different animation than the default
  override var preferredStatusBarUpdateAnimation: UIStatusBarAnimation {
    return .slide
  }
}

Update barsHidden somewhere in the code and call setNeedsStatusBarAppearanceUpdate()