Change the color of the text in the title bar of the navigation bar when "prefersLargeTitles" is set to true

I have a requirement in which I have to use a UINavigationBar with a red big header.

I currently 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 this does not really tint the title bar in 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’m not entirely sure that this is a mistake, because at the time of writing this article we are still in the first beta version, or if this is deliberate behavior, mainly due to the fact that I do not have any of the Apple applications. way to get a great headline for any color i want?

+25
ios uikit ios11 uinavigationbar
source share
5 answers

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

largeTitleTextAttribute

Here is an example of code that you can add to your view. viewDidLoad method

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

enter image description here

Here is a sample code and a screenshot without a set of large TitleTextAttributes, but barStyle is set to .black

  navigationController?.navigationBar.barStyle = .black 

enter image description here

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

  navigationController?.navigationBar.barStyle = .default 

enter image description here

+53
source share

Not sure if this is a bug in beta 1 and 2, but here is a way to set the color. This is a bit of a “hacker" workaround, but it should work until Apple decides to. In versions of Objective-C and Swift, this code is used in the viewDidAppear: method.

Objective-C:

 dispatch_async(dispatch_get_main_queue(), ^{ for (UIView *view in self.navigationController.navigationBar.subviews) { NSArray <__kindof UIView *> *subviews = view.subviews; if (subviews.count > 0) { UILabel *label = subviews[0]; if (label.class == [UILabel class]) { [label setTextColor:[UIColor redColor]]; } } } }); 

Swift:

 DispatchQueue.main.async { for view in self.navigationController?.navigationBar.subviews ?? [] { let subviews = view.subviews if subviews.count > 0, let label = subviews[0] as? UILabel { label.textColor = UIColor.red } } } 
+5
source share

The way you do this in iOS 13 has changed, now you are using the 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 
+3
source share

Here is the working code for using large headers and sets the text color of small and large headers to white, both on iOS11 + and on older versions of iOS.

 // Will apply to versions before iOS 11 navigationController?.navigationBar.titleTextAttributes = [ NSAttributedStringKey.foregroundColor: UIColor.white ] if #available(iOS 11.0, *) { navigationController?.navigationBar.prefersLargeTitles = true navigationController?.navigationBar.largeTitleTextAttributes = [ NSAttributedStringKey.foregroundColor: UIColor.white ] } 

(There used to be a bug in Xcode, but now it's fixed)

+1
source share

When using storyboards, simply change the color of the “Large Text Attributes” title in the Attribute Inspector of the navigation bar:

enter image description here

0
source share

All Articles