简体   繁体   English

如何在iOS13上设置嵌入导航controller的状态栏样式?

[英]How to set style of the statusbar embedded in a navigation controller on iOS13?

as many iOS devs out there i'm facing some issues with iOS 13 update.与许多 iOS 开发人员一样,我正面临 iOS 13 更新的一些问题。 One of these was the different management of the status bar style其中之一是状态栏样式的不同管理

On iOS 12 i used to set the navigation bar style like this在 iOS 12 上,我曾经像这样设置导航栏样式

self.navigationController?.navigationBar.barStyle = .black

which affects the status bar style, setting it to white (because the navigation bar style is black);影响状态栏样式,设置为白色(因为导航栏样式为黑色); but it doesn't seem to work on iOS 13, i guess it has something to deal with但它似乎不适用于 iOS 13,我想它需要处理一些事情

UINavigationBarAppearance() 

class class

I configured my navigation bar for each ViewController like this:我为每个 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

so far so good, but到目前为止还不错,但是

self.navigationController?.navigationBar.barStyle = .black

works just on iOS 12, nothing happens on iOS 13 the status bar still looks black instead of white仅适用于 iOS 12,在 iOS 13 上没有任何反应,状态栏仍然看起来是黑色而不是白色

Did anyone face this issue?有人遇到过这个问题吗?

Finally i figured out!最后我想通了!

the magic code to set a light status bar text is:设置灯光状态栏文本的魔术代码是:

 self.navigationController?.navigationBar.overrideUserInterfaceStyle = .dark

of course if you want to change to dark text i have to set it to.light.当然,如果您想更改为深色文本,我必须将其设置为.light。

Some things to notice:需要注意的一些事项:

  • This code:这段代码:

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

    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 . self.navigationController?.navigationBar.barStyle =.black 但把它放在 UINavigationBarAppearance() 设置之后和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

Hope it helps;希望能帮助到你; ;) ;)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM