如何更改导航项标题颜色

我想整天改变导航栏的标题颜色,但它不工作。这是我的代码:

var user: User? {
didSet {
navigationItem.title = user?.name


observeMessages()
}
}

我使用 didSet 在导航标题上显示标题。

101355 次浏览

如果您设置了用户,可能您正在设置用户的 name 变量并期望程序输入 ddSet,那么将调用 ddSet。 尝试设置用户。

如果你想在导航标题改为用户名时改变文本的颜色,只需要调用这个代码。

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
}

导航栏的标题颜色可以在故事板中更改。

转到 导航控制器 > 导航条属性检查员,在 标题颜色菜单中设置所需的颜色。


enter image description here

在你的代码中加入这个. 。

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
}

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]

IOS13的解决方案

要自定义导航栏的外观,您需要使用 UINavigationBarAppearance:

let appearance = UINavigationBarAppearance()
appearance.titleTextAttributes = [.foregroundColor: UIColor.red]
appearance.largeTitleTextAttributes = [.foregroundColor: UIColor.red]


navigationItem.standardAppearance = appearance
navigationItem.scrollEdgeAppearance = appearance

如果您将导航栏的标题设置为更喜欢大标题,如下所示:

navigationBar.prefersLargeTitles = true

然后,您需要使用 largeTitleTextAttritribute 属性,而不是 titleTextAttritribute 属性。如果将导航标题设置为大标题,则 titleTextAttribute 不是要使用的正确属性。使用 largeTitleTextAttritribute 属性,如下所示:

navigationBar.largeTitleTextAttributes = [.foregroundColor: UIColor.white]

斯威夫特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中调用 UINavigationBar.appearance().customNavigationBar()didFinishLaunchingWithOptions中的函数

Swift 4

创建项目并使用 ViewController 进行测试,使其易于使用

import UIKit
class ProfileViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
configureNavigationBar()
          

}
func configureNavigationBar() {
navigationItem.title = "Profile"
let textChangeColor =[NSAttributedString.Key.foregroundColor:UIColor.black]
navigationController?.navigationBar.titleTextAttributes = textAttributes
navigationItem.rightBarButtonItem?.tintColor = .white
navigationItem.rightBarButtonItem = UIBarButtonItem(image: #imageLiteral(resourceName: "arrow_right").withRenderingMode(.alwaysOriginal), style: .plain, target: self, action: #selector(handleDismiss))
navigationController?.navigationBar.prefersLargeTitles = true
navigationItem.rightBarButtonItem?.tintColor = .white
}
}

对于 iOS13,你必须改变外观属性中的颜色,而对于老版本的 iOS,你可以直接在导航栏属性中改变颜色。

if #available(iOS 13.0, *) {
navigationController?.navigationBar.standardAppearance.titleTextAttributes = [.foregroundColor: UIColor.white]
} else {
navigationController?.navigationBar.titleTextAttributes = [.foregroundColor: UIColor.white]
}

您可以将这行代码添加到 func 中的 AppGenerate 中

didFinishLaunchingWithOptions

您将添加到更改标题颜色的代码:

UINavigationBar.appearance().titleTextAttributes = [NSAttributedString.Key.foregroundColor : UIColor(red: 0.7415059209, green: 0.5448099971, blue: 0.5051562786, alpha: 1)]

如果要更改 backButton 颜色,也可以添加此行

  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)

注意: 您可以根据所需的颜色更改值 祝你编程愉快

目标 c:

[UINavigationBar appearance] setTitleTextAttributes:
[NSDictionary dictionaryWithObjectsAndKeys:
[UIColor blackColor], NSForegroundColorAttributeName,
[UIFont fontWithName:@"ArialMT" size:16.0], NSFontAttributeName,nil]];