我整天都在想如何改变导航栏标题颜色,但是效果不理想。这是我的代码:
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
}
要自定义导航栏的外观,您需要使用 UINavigationBarAppearance
:
let appearance = UINavigationBarAppearance()
appearance.titleTextAttributes = [.foregroundColor: UIColor.red]
appearance.largeTitleTextAttributes = [.foregroundColor: UIColor.red]
navigationItem.standardAppearance = appearance
navigationItem.scrollEdgeAppearance = appearance
largeTitleTextAttributes
并使标题发生了改变。 - emrcftci在Storyboard中可以更改导航栏的标题颜色。
前往导航控制器的属性检查器 > 导航栏并在标题颜色菜单中设置所需颜色。
Swift 4
先设置这个
navigationController?.navigationBar.barStyle = .default
那么其中一个应该会起作用
navigationBar.largeTitleTextAttributes = [NSAttributedStringKey.foregroundColor: UIColor.red]
或navigationBar.titleTextAttributes = [NSAttributedStringKey.foregroundColor: UIColor.red]
Swift 5
navigationBar.largeTitleTextAttributes = [NSAttributedString.Key.foregroundColor: UIColor.red]
或者navigationBar.titleTextAttributes = [NSAttributedString.Key.foregroundColor: UIColor.red]
iOS 13需要在外观属性中更改颜色,而对于旧版本的iOS,则可以直接在导航栏属性中进行更改。
if #available(iOS 13.0, *) {
navigationController?.navigationBar.standardAppearance.titleTextAttributes = [.foregroundColor: UIColor.white]
} else {
navigationController?.navigationBar.titleTextAttributes = [.foregroundColor: UIColor.white]
}
在Objective-C中:
[UINavigationBar appearance] setTitleTextAttributes:
[NSDictionary dictionaryWithObjectsAndKeys:
[UIColor blackColor], NSForegroundColorAttributeName,
[UIFont fontWithName:@"ArialMT" size:16.0], NSFontAttributeName,nil]];
如果您设置了用户,那么 didSet 将被调用,也许您正在设置用户的名称变量并期望程序进入 didSet。请尝试设置用户。
如果您想在导航标题更改为用户名称时更改文本的颜色,请调用此代码。
navigationController?.navigationBar.titleTextAttributes = [NSForegroundColorAttributeName : UIColor.red]
var user: User? {
didSet {
navigationItem.title = user?.name
}
}
override func viewDidLoad() {
super.viewDidLoad()
let newUser = User()
newUser.name = "John Doe"
user = newUser
}
您可以在AppDelegate的func中添加此行代码
didFinishLaunchingWithOptions
你需要添加的代码来改变标题颜色:
UINavigationBar.appearance().titleTextAttributes = [NSAttributedString.Key.foregroundColor : UIColor(red: 0.7415059209, green: 0.5448099971, blue: 0.5051562786, alpha: 1)]
如果您想要更改返回按钮的颜色,也可以添加此行代码
UINavigationBar.appearance().tintColor = #colorLiteral(red: 0.7415059209, green: 0.5448099971, blue: 0.5051562786, alpha: 1)
最后,您可以添加此行以更改背景颜色:
UINavigationBar.appearance().barTintColor = #colorLiteral(red: 0.2000651062, green: 0.1960035861, blue: 0.2000851929, alpha: 1)
注意:您可以更改取决于所需颜色的值,祝您编码愉快。
UINavigationBarAppearance()
进行设置,但添加下面的代码以及外观对象可以解决问题:
UINavigationBar.appearance().tintColor = UIColor.white
- KingKongCodernavigationBar.prefersLargeTitles = true
如果你将导航栏标题设置为大标题,则需要使用largeTitleTextAttributes属性,而不是titleTextAttributes属性。大标题时,使用titleTextAttribute属性是不正确的。应该这样使用largeTitleTextAttributes属性:
navigationBar.largeTitleTextAttributes = [.foregroundColor: UIColor.white]
Swift 5/Xcode 11
将下面的代码作为UINavigationBar的扩展编写:
extension UINavigationBar {
func customNavigationBar() {
// color for button images, indicators and etc.
self.tintColor = UIColor.Custom.mainAppColor
// color for background of navigation bar
// but if you use larget titles, then in viewDidLoad must write
// navigationController?.view.backgroundColor = // your color
self.barTintColor = .white
self.isTranslucent = false
// for larget titles
self.prefersLargeTitles = true
// color for large title label
self.largeTitleTextAttributes = [NSAttributedString.Key.foregroundColor: UIColor.someColor]
// color for standard title label
self.titleTextAttributes = [NSAttributedString.Key.foregroundColor: UIColor.someColor]
// remove bottom line/shadow
self.setBackgroundImage(UIImage(), for: .default)
self.shadowImage = UIImage()
}
}
然后在 AppDelegate.swift
的 didFinishLaunchingWithOptions
函数中调用 UINavigationBar.appearance().customNavigationBar()
viewDidLoad
中编码时它没有起作用,直到我看到你的完整源代码。顺便说一下,当我在viewDidLoad
中编码时,我成功了,请看我的代码:override func viewDidLoad() { super.viewDidLoad() user = "holla" let textAttributes = [NSForegroundColorAttributeName:UIColor.red] navigationController?.navigationBar.titleTextAttributes = textAttributes }
- roy