ios 如何更改导航项标题颜色

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/43706103/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-31 09:05:39  来源:igfitidea点击:

how to change navigationitem title color

iosswift

提问by u7757141

I think all day to change the navigation Bar title color, but it doesn't work. this is my code:

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

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

         observeMessages()
    }
}

I use didSet to show the title on the navigation title.

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

回答by roy

Add this in your code . .

将此添加到您的代码中。.

let textAttributes = [NSForegroundColorAttributeName:UIColor.red]
navigationController?.navigationBar.titleTextAttributes = textAttributes

SWIFT 4:

快速 4:

let textAttributes = [NSAttributedStringKey.foregroundColor:UIColor.red]
navigationController?.navigationBar.titleTextAttributes = textAttributes

SWIFT 4.2+:

SWIFT 4.2+:

let textAttributes = [NSAttributedString.Key.foregroundColor:UIColor.red]
navigationController?.navigationBar.titleTextAttributes = textAttributes

Keeping all the other attributes of the title:If you just want change the color you could do like this:

保留标题的所有其他属性:如果您只想更改颜色,您可以这样做:

if var textAttributes = navigationController?.navigationBar.titleTextAttributes {
    textAttributes[NSAttributedString.Key.foregroundColor] = UIColor.red
    navigationController?.navigationBar.titleTextAttributes = textAttributes
}

回答by Tzar

The title color of Navigation Bar can be changed in Storyboard.

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

Go to Attributes inspectorof Navigation Controller> Navigation Barand set the desired color in Title Colormenu.

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



enter image description here

在此处输入图片说明

回答by ChaosTong

Swift 4

斯威夫特 4

set this first

首先设置这个

navigationController?.navigationBar.barStyle = .default

then one of those should work

那么其中之一应该可以工作

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

or

或者

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

Swift 5

斯威夫特 5

navigationBar.largeTitleTextAttributes = [NSAttributedString.Key.foregroundColor: UIColor.red]

or

或者

navigationBar.titleTextAttributes = [NSAttributedString.Key.foregroundColor: UIColor.red]

回答by M Reza Farahani

Solution for iOS 13

适用于 iOS 13 的解决方案

To customize the appearance of a navigation bar you need to use UINavigationBarAppearance:

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

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

navigationItem.standardAppearance = appearance
navigationItem.scrollEdgeAppearance = appearance

回答by Chuy47

For iOS 13 you have to change the color in the appearance property and for older iOS versions you can do it directly in the navigation bar property.

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

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

回答by J. Koush

The didSet is called if your set the user, maybe you're setting the user's name variable and expecting the program to enter didSet. try setting the user.

如果您设置用户,则调用 didSet,也许您正在设置用户的名称变量并期望程序输入 didSet。尝试设置用户。

And if you want to change the color of the text when the navigation title is changed to the name of the user just call this code.

如果您想在导航标题更改为用户名称时更改文本颜色,只需调用此代码即可。

navigationController?.navigationBar.titleTextAttributes = [NSForegroundColorAttributeName : UIColor.red]

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
}

回答by Mat Christensen

If you set your navigation bar's title to prefer large titles, like so:

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

navigationBar.prefersLargeTitles = true

then you need to use the largeTitleTextAttributes property and not the titleTextAttributes property. If you set your nav title to be a large title, the titleTextAttribute is not the correct property to use. Use the largeTitleTextAttributes property, like so:

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

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

回答by Hyman Daniel

Swift 5/Xcode 11

斯威夫特 5/Xcode 11

Write this code as extension from UINavigationBar

将此代码编写为 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()
    }
}

then in AppDelegate.swiftcall UINavigationBar.appearance().customNavigationBar()in didFinishLaunchingWithOptionsfunction

然后在AppDelegate.swift通话UINavigationBar.appearance().customNavigationBar()didFinishLaunchingWithOptions功能

回答by islam XDeveloper

Swift 4

斯威夫特 4

create project and test this with ViewController easy to use

创建项目并使用易于使用的 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
                    }
}