如何更改navigationitem标题颜色

时间:2017-04-30 11:57:23

标签: ios swift

我认为整天都要更改导航栏标题颜色,但它不起作用。这是我的代码:

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

         observeMessages()
    }
}

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

10 个答案:

答案 0 :(得分:114)

在您的代码中添加此内容。

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

答案 1 :(得分:23)

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

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

enter image description here

答案 2 :(得分:5)

首先设置

navigationController?.navigationBar.barStyle = .default

然后其中一个应该工作

navigationBar.largeTitleTextAttributes = [NSAttributedStringKey.foregroundColor: UIColor.red]

navigationBar.titleTextAttributes = [NSAttributedStringKey.foregroundColor: UIColor.red]

答案 3 :(得分:5)

对于iOS 13,您必须在外观属性中更改颜色;对于较旧的iOS版本,您可以直接在导航栏属性中进行更改。

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

答案 4 :(得分:3)

如果将导航栏的标题设置为喜欢大标题,例如:

navigationBar.prefersLargeTitles = true

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

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

答案 5 :(得分:2)

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

答案 6 :(得分:2)

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

答案 7 :(得分:1)

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

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

navigationItem.standardAppearance = appearance
navigationItem.scrollEdgeAppearance = appearance

答案 8 :(得分:1)

雨燕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
                    }
}

答案 9 :(得分:0)

您只需在功能中的AppDelegate中添加此行代码

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)

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