Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Changing the text color of a navigation bar title when "prefersLargeTitles" is set to true

I have a requirement in which I have to use a UINavigationBar with a red large title.

Currently, I have the following code:

func prepareNavigationController() {     let navController = UINavigationController(rootViewController: self)     navController.navigationBar.prefersLargeTitles = true     navigationItem.searchController = UISearchController(searchResultsController: nil)     navigationItem.hidesSearchBarWhenScrolling = false     navController.navigationBar.titleTextAttributes = [NSAttributedStringKey.foregroundColor.rawValue: UIColor.red] } 

But it's not actually tinting the title label to red. This is the result:

Ignored title color

But changing prefersLargeTitles to false does the right thing, and my title is red.

navController.navigationBar.prefersLargeTitles = false

Tinted Title

I am not entirely sure if this is a bug since at the time of this writing we are still in the first beta, or if this is intentional behavior, mostly because I haven't any of Apple's apps color the large titles before. Is there any way to actually get the large title to have any color I want?

like image 241
Andy Ibanez Avatar asked Jun 18 '17 20:06

Andy Ibanez


People also ask

How do I change the color of my navigation bar text?

The text color of the navigation bar can be changed using two inbuilt classes: navbar-light: This class will set the color of the text to dark. This is used when using a light background color. navbar-dark: This class will set the color of the text to light.


2 Answers

There is a new UINavigationBar property "largeTitleTextAttribute" that should help with this.

largeTitleTextAttribute

Here is a sample code you can add to your view controllers viewDidLoad method

        navigationController?.navigationBar.largeTitleTextAttributes = [NSAttributedStringKey.foregroundColor: UIColor.blue] 

enter image description here

Here is a sample code and screenshot without the largeTitleTextAttributes set, but the barStyle is set to .black

        navigationController?.navigationBar.barStyle = .black 

enter image description here

Here is a screenshot without the largeTitleTextAttributes set, but the barStyle is set to .default

        navigationController?.navigationBar.barStyle = .default 

enter image description here

like image 71
assb10yr Avatar answered Sep 21 '22 07:09

assb10yr


The way you do this in iOS 13 has changed, you now use UINavigationBarAppearance class like this…

let appearance = UINavigationBarAppearance(idiom: .phone) appearance.largeTitleTextAttributes = [.foregroundColor: UIColor.systemRed] appearance.titleTextAttributes = [.foregroundColor: UIColor.systemRed] appearance.backgroundColor = .white navigationItem.standardAppearance = appearance navigationItem.scrollEdgeAppearance = appearance 
like image 27
Ashley Mills Avatar answered Sep 20 '22 07:09

Ashley Mills