status bar hidden in modal view (over fullscreen presentation) status bar hidden in modal view (over fullscreen presentation) ios ios

status bar hidden in modal view (over fullscreen presentation)


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

e.g.

toViewController.modalTransitionStyle = .coverVerticaltoViewController.modalPresentationStyle = .overFullScreentoViewController.modalPresentationCapturesStatusBarAppearance = truefromViewController.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 = .coverVerticaltoViewController.modalPresentationStyle = .fullScreenfromViewController.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.)


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}


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}