jrc jrc - 6 months ago 41
iOS Question

prefersStatusBarHidden isn't getting called

I have a standard Master-Detail Application, and I'm trying to conditionally show/hide the status bar.

Overriding

prefersStatusBarHidden()
in MasterViewController does nothing. It never even gets called.

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


Setting
UIViewControllerBasedStatusBarAppearance
in Info.plist doesn't help, presumably since
YES
is already the default value. Calling
setNeedsStatusBarAppearanceUpdate()
doesn't help either.

I am targeting iOS 9.

Answer

There is a little bit cleaner solution. There is a function childViewControllerForStatusBarHidden which is specifically designed to return a child view controller to which prefersStatusBarHidden should be forwarded.

So, it will be better to override it. It will look like this:

override func childViewControllerForStatusBarHidden() -> UIViewController? {
    if var topViewController = self.viewControllers.first {
        if let navigationController = topViewController as? UINavigationController {
            topViewController = navigationController.topViewController!
        }
        return topViewController
    }

    return super.childViewControllerForStatusBarHidden()
}

And probably you can even omit following. NavigationViewController has childViewControllerForStatusBarHidden() on it's own which will send it to child viewcontroller.

  if let navigationController = topViewController as? UINavigationController {
      topViewController = navigationController.topViewController!
  }