Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

preferredStatusBarStyle not respecting on iOS 13

Tags:

ios

swift

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

like image 201
Zack Shapiro Avatar asked Oct 02 '19 14:10

Zack Shapiro


People also ask

How do you override Preferredstatusbarstyle?

It is possible to override this on the UINavigationBar by setting overrideUserInterfaceStyle but this will result in the back list menu (from long press on back button) also having a dark mode style appearance.

Can we change status bar color in iOS?

You can change the status bar colour just with a single line of code. Just updated the markdown for iOS 13 and below.


2 Answers

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.

like image 120
RPK Avatar answered Sep 23 '22 13:09

RPK


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

like image 20
Zack Shapiro Avatar answered Sep 23 '22 13:09

Zack Shapiro