18

try to hide the status bar from a modal view.

already check several methods:

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

with / without self.setNeedsStatusBarAppearanceUpdate()

also

UIApplication.sharedApplication().setStatusBarHidden(true, withAnimation: .Fade)

but depreciated in iOS 9

this works in fullscreen presentation (modal segue presentation option) but note in over full screen which is my setting.

if you have any idea..

raphael
  • 777
  • 1
  • 9
  • 18
  • Please check http://stackoverflow.com/questions/32808593/setstatusbarhidden-withanimation-deprecated-in-ios-9, which I think addresses your issue. – geraldWilliam Nov 05 '15 at 14:10
  • hi, thanks but same issue. works on full screen presentation but not on over full screen setting.. – raphael Nov 05 '15 at 14:29
  • First go to plist and check if View controller-based status bar appearance is set to YES and set prefersStatusBarHidden() which you've tried. – Flipper Nov 05 '15 at 15:12
  • same issue, but thanks. – raphael Nov 05 '15 at 15:18

4 Answers4

58

For a non-fullscreen presentation of a View Controller, you need to use the modalPresentationCapturesStatusBarAppearance property.

e.g.

toViewController.modalTransitionStyle = .coverVertical
toViewController.modalPresentationStyle = .overFullScreen
toViewController.modalPresentationCapturesStatusBarAppearance = true

fromViewController.present(toViewController,
            animated: true,
            completion: nil)

For a fullscreen presentation of a View Controller, you need to:

  1. set the new VC's modalPresentationStyle.
  2. override prefersStatusBarHidden in the new VC
  3. set your app plist UIViewControllerBasedStatusBarAppearance value to YES

e.g.

toViewController.modalTransitionStyle = .coverVertical
toViewController.modalPresentationStyle = .fullScreen

fromViewController.present(toViewController,
            animated: true,
            completion: nil)

(Yes, status bar setting in iOS is pitifully bad. It's no wonder Stack Overflow has so many questions on the subject, and so many varied answers.)

Womble
  • 4,607
  • 2
  • 31
  • 45
8

To hide the status bar when doing an over full screen modal, you need to set this in viewDidLoad:

override func viewDidLoad() {
    super.viewDidLoad()    
    modalPresentationCapturesStatusBarAppearance = true
}

Then do the standard method to hide status bar:

override var prefersStatusBarHidden: Bool {
    return true
}
bnussey
  • 1,894
  • 1
  • 19
  • 34
2

Indeed for FullScreen status bar update called automatically, but not for OverFullScreen.

Furthermore in my case i was need to deal with navigation controller in stack, to pass ModalViewController as child:

extension UINavigationController {

    public override func childViewControllerForStatusBarHidden() -> UIViewController? {
        return self.visibleViewController
    }

    public override func childViewControllerForStatusBarStyle() -> UIViewController? {
        return self.visibleViewController
    }
}

Inside ModalViewController we manually update status bar, also in order to make it smooth we have to do that in viewWillDisappear, but at that point visibleViewController still ModalViewController, nothing left as to use internal bool statusBarHidden and update it accordingly

override func viewWillAppear(animated: Bool) {
    super.viewWillAppear(animated)
    self.statusBarHidden = true
    self.setNeedsStatusBarAppearanceUpdate()
}
override func viewWillDisappear(animated: Bool) {
    super.viewWillDisappear(animated)
    self.statusBarHidden = false
    self.setNeedsStatusBarAppearanceUpdate()
}
override func prefersStatusBarHidden() -> Bool {
    return self.statusBarHidden
}
Vadim Pavlov
  • 101
  • 1
  • 3
0

If you are using a storyboard and you want to hide/show the status bar, you can use this method on previous view controller:

override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
      UIApplication.shared.setStatusBarHidden(false, with: UIStatusBarAnimation.none)
}
kikettas
  • 1,682
  • 1
  • 24
  • 31