8

I'm working in my simulator on a single view app with a dark background. It's a UIViewController wrapped in a UINavigationController.

In my view controller I have override var preferredStatusBarStyle: UIStatusBarStyle { .lightContent }

In my info.plist I have View controller-based status bar appearance = YES

And yet when I run it it shows white for a second and then jumps to having black text.

What's going on here? Is there a fix?

Edit: I've tried .default, .lightContent and .darkContent just to be sure, nothing works

Zack Shapiro
  • 6,648
  • 17
  • 83
  • 151
  • 1
    This has nothing to do with iOS 13. Overriding `preferredStatusBarStyle` when you're wrapped in a navigation controller has _never_ been the right way. – matt Oct 02 '19 at 15:28
  • Can you leave your comment as an answer and I'll mark as correct? Thanks Matt – Zack Shapiro Oct 03 '19 at 14:19
  • No thanks. I’ve dealt with this at https://stackoverflow.com/questions/58210536/ios-13-setting-status-bar-text-color-from-within-uinavigationcontroller/ and https://stackoverflow.com/a/52457515/341994. – matt Oct 03 '19 at 15:48
  • Thank you @Matt this the correct answer and good explanation https://stackoverflow.com/a/52457515/341994 – H.Gadou Mar 03 '21 at 09:53

2 Answers2

16

I recently ran into this problem and these extensions seemed to fix the issue.

extension UITabBarController {
    open override var childForStatusBarStyle: UIViewController? {
        return selectedViewController?.childForStatusBarStyle ?? selectedViewController
    }
}

extension UINavigationController {
    open override var childForStatusBarStyle: UIViewController? {
        return topViewController?.childForStatusBarStyle ?? topViewController
    }
}

I just put them into a file called UIViewController+StatusBar.swift and included it in the project.

RPK
  • 1,830
  • 14
  • 30
  • Thank you! I'm going to edit my question to mention my VC is in a UINavigationController so your second extension did the trick. I've seen this question a few other times in relation to the tab bar – Zack Shapiro Oct 02 '19 at 14:52
  • 2
    This is wrong. The way to change the status bar style when you have a navigation bar is to set the navigation bar's `barStyle`. See my answer at https://stackoverflow.com/a/52457515/341994 – matt Oct 02 '19 at 15:16
3

The correct answer referenced by @matt is navigationController?.navigationBar.barStyle = .lightContent in viewDidLoad.

Zack Shapiro
  • 6,648
  • 17
  • 83
  • 151
  • 4
    It's not `.lightContent` but `.black` if you want a white status bar. `UIBarStyle.lightContent`doesn't exist. – Skoua Feb 13 '20 at 23:41