3
votes

So before iOS 13, I was setting the NavigationController.NavigationBar.BarStyle to control the colour of the text int he status bar. But now witht he new UINavigationBarAppearance, is there anyway to control that? I've already tried overriding preferedStatusBarStyle, and ensured my plist contains the setting as well but as far as I can tell this isn't honored if your view controller sits inside a UINavigationController.

So now for iOS 13 I'm setting my navigation bar's appearance using the UINavigationBarAppearance and setting the standardAppearance and scrollEdgeAppearance.

I'm also trying to set NavigationController.NavigationBar.BarStyle to UIBarStyle.Black. It works the first time, but for some reason when I go back and go navigate to the page again setting BarStyle no longer has any effect.

What is the correct way to set the text color?

3

3 Answers

2
votes

The barStyle does still work under limited conditions. You can use the barStyle or UIBarAppearance but not both, and you can’t use the barStyle if you use large titles.

Of course you could trivially solve the problem just by subclassing UINavigationController. A bit hacky, but easy.

override var childForStatusBarStyle : UIViewController? {
    return self.topViewController
}

What you are expected to do in iOS 13, though, is leave the status bar style alone and let it change automatically in response to the user interface style (light or dark mode). Use a light bar color in light mode and a dark bar color in dark mode and all will be well.

1
votes

You can change the status bar text color by using this extension:

extension UIApplication {

    enum ColorMode {
        case dark, light, customColorLowerThanIOS13(_ color: UIColor)
    }

    func setStatusBarTextColor(_ mode: ColorMode) {
        if #available(iOS 13.0, *) {
            guard let appDelegate = delegate as? AppDelegate else { return }

            var style: UIUserInterfaceStyle

            switch mode {
                case .dark:
                    style = .dark
                default:
                    style = .light
            }

            appDelegate.window?.overrideUserInterfaceStyle = style
        } else {
            if let statusBar = UIApplication.shared.value(forKey: "statusBar") as? UIView {
                var color: UIColor

                switch mode {
                    case .dark:
                        color = .white
                    case .light:
                        color = .black
                    case .customColorLowerThanIOS13(let customColor):
                        color = customColor
                }

                statusBar.setValue(color, forKey: "foregroundColor")
            }
        }
    }

}

Using:

UIApplication.shared.setStatusBarTextColor(.dark)
UIApplication.shared.setStatusBarTextColor(.light)
UIApplication.shared.setStatusBarTextColor(.customColorLowerThanIOS13(.red))
-1
votes

In Info.plist, if added the Boolean Property UIViewControllerBasedStatusBarAppearance and set its value to False

<key>UIViewControllerBasedStatusBarAppearance</key>
<false/>

And you can change StatusBar TextColor by follow way :

UIApplication.SharedApplication.SetStatusBarStyle(UIStatusBarStyle.LightContent, true);

In addition , this method should be invoked after View loaded totally . From IOS 13 ,Apple separates the UI from the Controller, which may affect the appearance of the PreferredStatusBarStyle in the Controller. Before this problem is handled, you can use UIApplication.SharedApplication.StatusBarStyle to set it.