0
votes

I working on a huge app loads of view controllers, the app currently sets in status bar style in the plist using:

Status bar style = UIStatusBarStyleLightContent
View controller-based status bar appearance = NO

New features require me to have some status bars in the dark default style.

So to summarize I need all the app with a default .lightContent and about 10VCs with an override to .default/dark

To start with I need to set the plist to:

View controller-based status bar appearance = YES

but once I do this many of the status bars in the app change to the .default / dark style.

I can change this style in every VC using:

 override var preferredStatusBarStyle: UIStatusBarStyle {
    return .lightContent
}

but in an app this big, this would be impractical.

I've tried quite a few extensions to override behavior, but since these are instance methods they can't be overriden like this:

    extension UIViewController
    {
        override open var preferredStatusBarStyle: UIStatusBarStyle {
        return .lightContent
         }
     }

You can use this extension:

extension UINavigationController {
    override open var preferredStatusBarStyle : UIStatusBarStyle {
        return topViewController?.preferredStatusBarStyle ?? .default
    }
}

But it only works on VC's in NavControllers and you need to set every single rootVC to .lightContent

2
When you changed "View controller-based status bar appearance" to YES, did you leave the "Status bar style = UIStatusBarStyleLightContent" setting in place or did you remove it?rmaddy
I left it as "UIStatusBarStyleLightContent"Adam
You can use your extension. Just check if topViewController is one of the controllers and return default, otherwise return lightContent.Don

2 Answers

0
votes

The extension you provided should work.

extension UINavigationController {
  open override var preferredStatusBarStyle: UIStatusBarStyle {
    switch topViewController {
      case is DarkContentVC1, is DarkContentVC2, is DarkContentVC3:
        return .default
      default:
        return .lightContent
    }
  }
}
0
votes

After much research it turns out that setting View controller-based status bar appearance to YES changes the paradigm of how the status bar is manipulated. It set's it to default which automatically configures it to the expected style, it also allows for overriding within individual VC's.

To obtain the goal of setting the whole app to lightContent as the default style, the best solution would be to create a subclass of UIViewController for all VC's in your app with that functionality.

A second solution would be to manually change all the root view controllers.