I have a problem when it comes to the status bar and hiding it.
I have a BaseViewController that has a slide out menu. This BaseViewController is also the root controller of the application [as set inside AppDelegate]:
window = UIWindow()
window?.makeKeyAndVisible()
window?.rootViewController = BaseController()
As soon as I select a menu item the BaseViewController is populated by the corresponding ViewController [after I embed it into a navigation controller].
Menu item A: ViewControllerA
Menu item B: ViewControllerB
Menu item C: ViewControllerC
Say that I select the Menu Item A (the following code takes place inside BaseViewController):
let activeVC = UINavigationController(rootViewController: ViewControllerA())
view.addSubview(activeVC.view)
addChild(activeVC)
When I select another menu item (say item B), I first remove the previous active view controller (in this case item A) and then I add the ViewControllerB the same way as I did with ViewControllerA:
This is how I remove the previous active view controller:
activeVC.view.removeFromSuperview()
activeVC.removeFromParent()
I set the View controller-based status bar appearance to YES in plist control the appearance of the status bar in every view controller:
Then I go into the ViewController I want to hide the status bar and I add the following code:
override var prefersStatusBarHidden: Bool {
return true
}
If I want to hide the status bar inside any of the ViewController A, B, or C, I can't. Overriding the prefersStatusBarHidden and setting it to "true" will do nothing.
If I override the prefersStatusBarHidden and setting it to "true" into the BaseViewController, then the BaseViewController and also any of the ViewController A, B, and C will hide the status bar.
I want to be able to hide the status bar on ViewControllerB without hiding it on the rest. Also a million dollars, but I will settle with the solution!
Thanks in advance!
You'll need to override var childForStatusBarHidden: UIViewController?
for BaseController
and for UINavigationController
. For example:
override var childForStatusBarHidden: UIViewController? {
return children.first
}
and
extension UINavigationController {
open override var childForStatusBarHidden: UIViewController? {
return topViewController
}
}