ste*_*hen 5 uinavigationbar uinavigationcontroller ios swift ios13
当用户选择不同的主题样式时,我需要更改导航栏背景颜色。
但奇怪的是,用户选择“深色”模式后,进入后台,然后回到前台,如果用户想改回“浅色”模式,导航栏仍然是黑色风格,有一个“_UIVisualEffectBackdropView”保持黑暗。
但如果用户在进入后台之前选择“Light”模式,则一切正常。
我该如何修复这个错误?下面是代码和图片:
override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
switch indexPath.row {
case 0:
self.changeToLightColor()
default:
self.changeToDarkColor()
}
}
private func changeToLightColor() {
self.navigationController?.navigationBar.barStyle = .default
let textAttribute = [NSAttributedString.Key.foregroundColor: UIColor.systemBlue]
self.navigationController?.navigationBar.titleTextAttributes = textAttribute
}
private func changeToDarkColor() {
self.navigationController?.navigationBar.barStyle = .black
let textAttribute = [NSAttributedString.Key.foregroundColor: UIColor.systemGreen]
self.navigationController?.navigationBar.titleTextAttributes = textAttribute
}
Run Code Online (Sandbox Code Playgroud)
非常感谢您的帮助和提前答复!
小智 3
好吧,我花了一点时间来弄清楚如何解决这个问题,而且解决方案非常简单。
只需设置barTintColor您navigationBar需要的颜色即可。
private func changeToLightColor() {
self.navigationController?.navigationBar.barStyle = .default
//Set to white color
self.navigationController?.navigationBar.barTintColor = UIColor.white
let textAttribute = [NSAttributedString.Key.foregroundColor: UIColor.systemBlue]
self.navigationController?.navigationBar.titleTextAttributes = textAttribute
}
private func changeToDarkColor() {
self.navigationController?.navigationBar.barStyle = .black
//Set to black color
self.navigationController?.navigationBar.barTintColor = UIColor.black
let textAttribute = [NSAttributedString.Key.foregroundColor: UIColor.systemGreen]
self.navigationController?.navigationBar.titleTextAttributes = textAttribute
}
Run Code Online (Sandbox Code Playgroud)
我这样做之后,问题就消失了
| 归档时间: |
|
| 查看次数: |
3081 次 |
| 最近记录: |