With animations:

  • Solution 1:

https://stackoverzone.files.wordpress.com/2015/10/hide-status-bar.gif

class ViewController: UIViewController {
  var hideStatusBar = false
  
  @IBAction func buttonTapped(button: UIButton) {
          hideStatusBar = !hideStatusBar
          UIView.animateWithDuration(0.25) { () -> Void in
              self.setNeedsStatusBarAppearanceUpdate()
          }
  }

  override func preferredStatusBarUpdateAnimation() -> UIStatusBarAnimation {
      return .Fade
  }

  override func prefersStatusBarHidden() -> Bool {
          return hideStatusBar
  }
}

Ref: http://stackoverflow.com/questions/32808593/setstatusbarhidden-withanimation-deprecated-in-ios-9#answer-32808743

  • Solution 2:
@IBAction fun buttonTapped(button: UIButton) {
  UIApplication.shared.setStatusBarHidden(false, withAnimation: .Fade)
}

But this was deprecated on iOS 9 and later :[

What should I do if: "View controller-based status bar appearance" = NO
UIApplication.shared.statusBarHidden = false

But this solution without animation :[