如何在iOS13上设置导航控制器中嵌入的状态栏的样式?
像许多iOS开发者一样,我面临着iOS 13更新的一些问题。
其中之一是对状态栏样式的不同管理
在iOS 12上,我过去常常这样设置导航栏样式
self.navigationController?.navigationBar.barStyle = .black
这会影响状态栏样式,将其设置为白色(因为导航栏样式为黑色); 但它在iOS 13上似乎不起作用,我猜它有一些问题要处理
UINavigationBarAppearance()
类
我对每个视图控制器的导航栏配置如下:
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
但当然不推荐使用,所以我建议使用其他方法
您仍需要:
<<2-1]>, 但请将其放在UINavigationBarPresance()设置之后,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
希望能有所帮助!;)
相关文章