繁体   English   中英

为什么 NavigationBar 背景颜色不改变?

[英]Why the NavigationBar background color not change?

当用户选择不同的 Theme 样式时,我需要更改 navigationBar 背景颜色。

但奇怪的是,用户选择“Dark”模式后,进入backgorund,然后回到前台,如果用户想改回“Light”模式,导航栏仍然是黑色的,有一个“_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
}

在此处输入图像描述

非常感谢您的帮助和提前回答!

好吧,花了一点时间来弄清楚如何解决这个问题,解决方案非常简单。

只需将navigationBarbarTintColor设置为您需要的颜色即可。

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
}

我做了之后,问题就解决了

暂无
暂无

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

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