我整天想着改变导航栏标题的颜色,但不管用。这是我的代码:
var user: User? {
didSet {
navigationItem.title = user?.name
observeMessages()
}
}
我使用 didSet 在导航标题上显示标题。
最佳答案
将此添加到您的代码中。 .
let textAttributes = [NSForegroundColorAttributeName:UIColor.red]
navigationController?.navigationBar.titleTextAttributes = textAttributes
SWIFT 4:
let textAttributes = [NSAttributedStringKey.foregroundColor:UIColor.red]
navigationController?.navigationBar.titleTextAttributes = textAttributes
SWIFT 4.2+:
let textAttributes = [NSAttributedString.Key.foregroundColor:UIColor.red]
navigationController?.navigationBar.titleTextAttributes = textAttributes
保留标题的所有其他属性: 如果你只想改变颜色,你可以这样做:
if var textAttributes = navigationController?.navigationBar.titleTextAttributes {
textAttributes[NSAttributedString.Key.foregroundColor] = UIColor.red
navigationController?.navigationBar.titleTextAttributes = textAttributes
}
关于ios - 如何更改导航项标题颜色,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43706103/