iOS 13 Setting status bar text color from within UINavigationController

前端 未结 3 2040
轮回少年
轮回少年 2021-01-21 17:34

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 UINavigationBarAppearan

相关标签:
3条回答
  • 2021-01-21 17:52

    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.

    0 讨论(0)
  • 2021-01-21 18:00

    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))
    
    0 讨论(0)
  • 2021-01-21 18:03

    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.

    0 讨论(0)
提交回复
热议问题