弹出后,UINavigationBar文本颜色保持白色

时间:2018-05-17 10:09:18

标签: ios swift

我正在将视图控制器推送到我的导航控制器,该控制器将导航栏设置为具有透明背景和白色文本。弹出视图时,应将导航栏恢复为以前的样子 - 白色背景和黑色文本。但是,这并没有发生。我正在推动的视图的代码:

override func viewWillAppear(_ animated: Bool) {
    super.viewWillAppear(animated)
    navigationController?.navigationBar.shadowImage = UIImage()
    navigationController?.navigationBar.barStyle = .black
    navigationController?.navigationBar.tintColor = .white
    navigationController?.navigationBar.setBackgroundImage(UIImage(), for: .default)
    navigationController?.navigationBar.isTranslucent = true
    navigationController?.navigationBar.backgroundColor = .clear
    navigationController?.view.backgroundColor = .clear
    setNeedsStatusBarAppearanceUpdate()
}

override func viewWillDisappear(_ animated: Bool) {
    super.viewWillDisappear(animated)
    navigationController?.navigationBar.shadowImage = nil
    navigationController?.navigationBar.barStyle = .default
    navigationController?.navigationBar.tintColor = .black
    navigationController?.navigationBar.setBackgroundImage(nil, for: .default)
    navigationController?.navigationBar.isTranslucent = false
    navigationController?.navigationBar.backgroundColor = .white
    navigationController?.view.backgroundColor = .white
    setNeedsStatusBarAppearanceUpdate()
}

当我弹回根视图控制器时,标题的颜色为白色。这应该是黑色的。为什么会这样?

1 个答案:

答案 0 :(得分:1)

实际上,安东尼斯(Antons)的建议是正确的,this post实际上帮助我解决了同样的怪异行为,但是仍然有一个实现步骤:

从帖子中,您将看到建议使用以下代码:

 let titleDict: NSDictionary = [NSAttributedString.Key.foregroundColor: UIColor.black]
        self.navigationController!.navigationBar.titleTextAttributes = (titleDict as! [NSAttributedString.Key : Any])

但是您需要记住将其放在viewDidLoad函数上。

希望它对某人有帮助:)