iOS 13 Setting status bar text color from within UINavigationController

前端 未结 3 2039
轮回少年
轮回少年 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 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))
    

提交回复
热议问题