gpt4 book ai didi

ios - 如何在 iOS13 上设置导航 Controller 中嵌入的状态栏的样式?

转载 作者:搜寻专家 更新时间:2023-11-01 06:50:02 26 4
gpt4 key购买 nike

许多 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
    }

    虽然它应该将整个 View 和 subview 设置为深色,但似乎不会影响状态栏。

  • 您还可以使用:

    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

希望对您有所帮助! ;)

关于ios - 如何在 iOS13 上设置导航 Controller 中嵌入的状态栏的样式?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58125973/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com