问题

尽管有很多iOS开发器,我面临一些iOS 13更新的问题. 其中之一是状态栏风格的不同管理

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

 self.navigationController?.navigationBar.barStyle = .black
 

影响状态条风格,将其设置为白色(因为导航栏风格为黑色); 但它似乎不适用于iOS 13,我想它有一些需要处理的东西

 UINavigationBarAppearance() 
 

班级

我为每个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
 

到目前为止这么好,但

 self.navigationController?.navigationBar.barStyle = .black
 

只在iOS 12上工作,在iOS 13上没有任何反应,状态栏仍然看起来黑色而不是白色

有没有人面临这个问题?

  最佳答案

最后我想出来了!

设置轻状态条文的魔术代码是:

  self.navigationController?.navigationBar.overrideUserInterfaceStyle = .dark
 

当然,如果你想更改为暗文本,我必须将其设置为.light.

有些事情需要注意:

  • 这段代码:

     if #available(iOS 13.0, *) {
        overrideUserInterfaceStyle = .dark
    }
     

    虽然它应该将整个视图和子视图设置为暗,但似乎并不影响状态栏.

  • 您还可以使用:

    UIApplication.shared.statusBarStyle = UIStatusBarStyle.lightContent

    但当然已弃用,所以我建议其他方法

  • 你仍然需要:

    self.navigationController?.navigationBar.barStyle = .black, 但是把它放在 UINavigationBarAppearance() 设置之后和 self.navigationController?.navigationBar.overrideUserInterfaceStyle = .dark 之后。

最终代码将如下所示:

 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
 

希望它有帮助!

  相同标签的其他问题

iosswiftuinavigationcontrollerstatusbarios13