如何更改导航栏标题颜色?

80

我整天都在想如何改变导航栏标题颜色,但是效果不理想。这是我的代码:

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

         observeMessages()
    }
}

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

11个回答

206

在你的代码中添加以下内容...

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
}

你很棒,伙计。但是,当我在viewDidLoad上编写以下代码时:navigationController?.navigationBar.titleTextAttributes = [NSForegroundColorAttributeName : UIColor.red] 它不起作用。 - Edward
感谢@熊天宇,我无法确定为什么你在viewDidLoad中编码时它没有起作用,直到我看到你的完整源代码。顺便说一下,当我在viewDidLoad中编码时,我成功了,请看我的代码:override func viewDidLoad() { super.viewDidLoad() user = "holla" let textAttributes = [NSForegroundColorAttributeName:UIColor.red] navigationController?.navigationBar.titleTextAttributes = textAttributes } - roy
适用于tvOS的代码,可更改UIViewController的标题。 - dar512
如果您的导航栏标题使用自定义字体,则此解决方案有效。使用答案“保留标题的所有其他属性”正是我所需要的。 - Andrew

45

iOS 13的解决方案

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

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

navigationItem.standardAppearance = appearance
navigationItem.scrollEdgeAppearance = appearance

1
谢谢,正在工作! - Senocico Stelian
5
来到这里的任何因为iOS15的用户,这是正确的答案 :) 我添加了largeTitleTextAttributes并使标题发生了改变。 - emrcftci

45

在Storyboard中可以更改导航栏的标题颜色。

前往导航控制器属性检查器 > 导航栏并在标题颜色菜单中设置所需颜色。


图片描述


27

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]

10

iOS 13需要在外观属性中更改颜色,而对于旧版本的iOS,则可以直接在导航栏属性中进行更改。

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

7

在Objective-C中:

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

4

如果您设置了用户,那么 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
}

那么,我应该设置用户名的颜色吗? - Edward
J.Koush非常感谢,我知道这个命令:navigationController?.navigationBar.titleTextAttributes = [NSForegroundColorAttributeName : UIColor.red] 但它仍然没有改变... - Edward

4

您可以在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 - KingKongCoder
很高兴与您一起工作,@kingkongCoder。 - Menaim

4
如果您将导航栏标题设置为偏好较大的标题,则可以这样操作:
navigationBar.prefersLargeTitles = true

如果你将导航栏标题设置为大标题,则需要使用largeTitleTextAttributes属性,而不是titleTextAttributes属性。大标题时,使用titleTextAttribute属性是不正确的。应该这样使用largeTitleTextAttributes属性:

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

4

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.swiftdidFinishLaunchingWithOptions 函数中调用 UINavigationBar.appearance().customNavigationBar()


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接