如何设置嵌入在 iOS13 导航控制器中的状态栏的样式?

mat*_*x87 3 statusbar uinavigationcontroller ios swift ios13

与许多 iOS 开发人员一样,我在 iOS 13 更新方面遇到了一些问题。其中之一是状态栏样式的不同管理

在 iOS 12 上,我曾经像这样设置导航栏样式

self.navigationController?.navigationBar.barStyle = .black
Run Code Online (Sandbox Code Playgroud)

影响状态栏样式,设置为白色(因为导航栏样式为黑色);但它似乎不适用于 iOS 13,我想它需要处理

UINavigationBarAppearance() 
Run Code Online (Sandbox Code Playgroud)

班级

我为每个 ViewController 配置了我的导航栏,如下所示:

if #available(iOS 13.0, *) {
            let navBarAppearance = UINavigationBarAppearance()
            navBarAppearance.configureWithOpaqueBackground()
            navBarAppearance.accessibilityTextualContext = .sourceCode
            navBarAppearance.titleTextAttributes = [.foregroundColor: UIColor.white]
            navBarAppearance.largeTitleTextAttributes = [.foregroundColor: UIColor.white]
            navBarAppearance.backgroundColor = .brownCircles
            navBarAppearance.shadowImage = nil // remove navigationBar Bottom border
            navBarAppearance.shadowColor = nil // remove navigationBar Bottom border

            self.navigationController?.navigationBar.standardAppearance = navBarAppearance
            self.navigationController?.navigationBar.compactAppearance = navBarAppearance
            self.navigationController?.navigationBar.scrollEdgeAppearance = navBarAppearance

        } else {
            self.navigationController?.navigationBar.barTintColor = .blue
            self.navigationItem.title = NSLocalizedString(kTitle, comment: kTitle.capitalized)
        }
self.navigationController?.navigationBar.barStyle = .black
Run Code Online (Sandbox Code Playgroud)

到目前为止一切顺利,但是

self.navigationController?.navigationBar.barStyle = .black
Run Code Online (Sandbox Code Playgroud)

仅适用于 iOS 12,在 iOS 13 上没有任何反应,状态栏仍然看起来是黑色而不是白色

有没有人遇到过这个问题?

mat*_*x87 7

Finally i figured out!

the magic code to set a light status bar text is:

 self.navigationController?.navigationBar.overrideUserInterfaceStyle = .dark
Run Code Online (Sandbox Code Playgroud)

of course if you want to change to dark text i have to set it to .light.

Some things to notice:

  • This code:

    if #available(iOS 13.0, *) {
        overrideUserInterfaceStyle = .dark
    }
    
    Run Code Online (Sandbox Code Playgroud)

    although it should set the entire view and subviews to dark, doesn't seem to affect the status bar.

  • You can also use:

    UIApplication.shared.statusBarStyle = UIStatusBarStyle.lightContent

    but of course is deprecated so i'd recommend other ways

  • You still need:

    self.navigationController?.navigationBar.barStyle = .black, but put it AFTER the UINavigationBarAppearance() settings and after the self.navigationController?.navigationBar.overrideUserInterfaceStyle = .dark.

Final code will look like this:

if #available(iOS 13.0, *) {
        let navBarAppearance = UINavigationBarAppearance()
        navBarAppearance.configureWithOpaqueBackground()
        navBarAppearance.accessibilityTextualContext = .sourceCode
        navBarAppearance.titleTextAttributes = [.foregroundColor: UIColor.white]
        navBarAppearance.largeTitleTextAttributes = [.foregroundColor: UIColor.white]
        navBarAppearance.backgroundColor = .brownCircles
        navBarAppearance.shadowImage = nil // remove navigationBar Bottom border
        navBarAppearance.shadowColor = nil // remove navigationBar Bottom border

        self.navigationController?.navigationBar.standardAppearance = navBarAppearance
        self.navigationController?.navigationBar.compactAppearance = navBarAppearance
        self.navigationController?.navigationBar.scrollEdgeAppearance = navBarAppearance

        self.navigationController?.navigationBar.overrideUserInterfaceStyle = .dark
} else {
        self.navigationController?.navigationBar.barTintColor = .blue
        self.navigationItem.title = NSLocalizedString(kTitle, comment: kTitle.capitalized)
}
self.navigationController?.navigationBar.barStyle = .black
Run Code Online (Sandbox Code Playgroud)

Hope it helps! ;)