为什么当我回到主 UIViewController
时,navigationBar 标题没有将其颜色更改为白色?这是简单的代码(viewWillAppear
,viewWillDisappear
),但它不起作用,当我回到这个 VC 上时,标题保持绿色。应用中的主色也是绿色:
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
UIApplication.shared.statusBarStyle = .lightContent
DispatchQueue.main.async {
self.navigationController?.navigationBar.titleTextAttributes = [NSAttributedStringKey.foregroundColor: UIColor.white, NSAttributedStringKey.font: UIFont(name: "Gotham-Medium", size: 20)!]
}
}
override func viewWillDisappear(_ animated: Bool) {
super.viewWillDisappear(animated)
UIApplication.shared.statusBarStyle = .default
navigationController?.navigationBar.titleTextAttributes = [NSAttributedStringKey.foregroundColor: UIColor.green, NSAttributedStringKey.font: UIFont(name: "Gotham-Medium", size: 20)!]
}
最佳答案
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
UIApplication.shared.statusBarStyle = .lightContent
DispatchQueue.main.async {
addTitleLabel()
}
}
override func viewWillDisappear(_ animated: Bool) {
super.viewWillDisappear(animated)
UIApplication.shared.statusBarStyle = .default
}
func addTitleLabel(){
var titleLabel: UILabel = UILabel()
titleLabel.textColor = .white
titleLabel.textAlignment = .center
titleLabel.text = "Home"
self.navigationItem.titleView = titleLabel
}
从 viewWillAppear 调用此方法。
关于ios - UINavigationBar 标题颜色,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48243361/