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.
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!
}
The answer is to override prefersStatusBarHidden()
starting from the window's root view controller. In a Master-Detail Application, this requires subclassing UISplitViewController
to forward the message down the view controller hierarchy.
Something like this:
override func prefersStatusBarHidden() -> Bool {
if var topViewController = self.viewControllers.first {
if let navigationController = topViewController as? UINavigationController {
topViewController = navigationController.topViewController!
}
return topViewController.prefersStatusBarHidden()
}
return super.prefersStatusBarHidden()
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With