【问题标题】:Why the NavigationBar background color not change?为什么 NavigationBar 背景颜色不会改变?
【发布时间】:2020-11-01 20:06:14
【问题描述】:

当用户选择不同的 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
}

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

【问题讨论】:

    标签: ios swift uinavigationcontroller uinavigationbar ios13


    【解决方案1】:

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

    只需将navigationBar 中的barTintColor 设置为您需要的颜色。

    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
    }
    

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-02-19
      • 1970-01-01
      • 1970-01-01
      • 2014-06-21
      • 2014-04-21
      • 2021-01-23
      • 2011-06-15
      • 1970-01-01
      相关资源
      最近更新 更多