简体   繁体   English

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

[英]Why the NavigationBar background color not change?

I need to change the navigationBar background color when the user chooses different Theme style.当用户选择不同的 Theme 样式时,我需要更改 navigationBar 背景颜色。

But the strange thing is, after the user chooses the "Dark" mode, then enter into backgorund, then back to foreground, if the user wants changing back to "Light" mode, the navigation bar is still in black style, there is a "_UIVisualEffectBackdropView" remains dark.但奇怪的是,用户选择“Dark”模式后,进入backgorund,然后回到前台,如果用户想改回“Light”模式,导航栏仍然是黑色的,有一个“_UIVisualEffectBackdropView”仍然是黑暗的。

But if the user chooses "Light" mode before enter into background, then everything works fine.但是如果用户在进入后台之前选择了“Light”模式,那么一切正常。

How can I fix this bug?我该如何修复这个错误? Below is the code & pic:下面是代码和图片:

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
}

在此处输入图像描述

Thanks very much for your help and answer in advance!非常感谢您的帮助和提前回答!

Well, it has taken a little time to figure out how to fix this, and the solution is very simple.好吧,花了一点时间来弄清楚如何解决这个问题,解决方案非常简单。

Just set barTintColor in navigationBar to color you need.只需将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
}

After I did it, the issue has gone我做了之后,问题就解决了

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

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