christostsang
christostsang

Reputation: 1841

Swift: Hide status bar on only one child view controller

I have a problem when it comes to the status bar and hiding it.

The setup

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()

Manipulating the status bar in each view controller separately:

I set the View controller-based status bar appearance to YES in plist control the appearance of the status bar in every view controller:

enter image description here

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
}

The problem

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.

What I want

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!

Upvotes: 0

Views: 1728

Answers (2)

Mussa Charles
Mussa Charles

Reputation: 4412

I had a use case where I had to present a ViewController nested inside a navBar controller the only way which worked for me is adding the following on initilization of a presented ViewController.

modalPresentationCapturesStatusBarAppearance = true

Upvotes: 0

beyowulf
beyowulf

Reputation: 15321

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
    }
}

Upvotes: 3

Related Questions