How to change the color of the navigation header

I think that all day you can change the title bar color of the navigation bar, but it does not work. this is my code:

var user: User? { didSet { navigationItem.title = user?.name observeMessages() } } 

I use hasSet to show the title in the navigation header.

+43
ios swift
source share
7 answers

Add this to your code. ,

 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 

Retaining all other title attributes: If you just want to change the color, you can do it like this:

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

The title bar color of the navigation bar can be changed in the storyboard.

Go to the Attributes inspector of the navigation controller> Navigation bar and set the desired color in the Title color menu.


enter image description here

+27
source share

set it first

 navigationController?.navigationBar.barStyle = .default 

then one of them should work

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

or

 navigationBar.titleTextAttributes = [NSAttributedStringKey.foregroundColor: UIColor.red] 
+8
source share

DidSet is called if your user is configured, perhaps you are setting a username variable and expect the program to enter didSet. try to install the user.

And if you want to change the color of the text when changing the name of the navigation to the username, just call this code.

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 } 
+3
source share

If you set the title of the navigation bar so that it prefers large headings, for example, like this:

 navigationBar.prefersLargeTitles = true 

then you need to use the largeTitleTextAttributes property, not the titleTextAttributes property. If you set the navigation title as a large title, titleTextAttribute is not the right property to use. Use the largeTitleTextAttributes property, for example, like this:

 navigationBar.largeTitleTextAttributes = [.foregroundColor: UIColor.white] 
+2
source share

In iOS 13, you need to use UINavigationBarAppearance to customize the appearance of the navigation bar:

 let appearance = UINavigationBarAppearance() appearance.titleTextAttributes = [.foregroundColor: UIColor.red] appearance.largeTitleTextAttributes = [.foregroundColor: UIColor.red] navigationItem.standardAppearance = appearance navigationItem.scrollEdgeAppearance = appearance 
+1
source share

Swift 5 / Xcode 11

Write this code as an extension from 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.swift call UINavigationBar.appearance().customNavigationBar() in the didFinishLaunchingWithOptions function

0
source share

All Articles