Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Customizing navigationBar in iOS7 - title color not working

I am trying to customize the navigationBar of a navigation controller in iOS7 and the title color is not changing. I am doing the following:

    [navigationController.navigationBar setBarTintColor:[UIColor colorWithRed:46.0f/256.0f green:46.0f/256.0f blue:46.0f/256.0f alpha:1]];     [navigationController.navigationBar setTranslucent:NO];     [navigationController.navigationBar setTitleTextAttributes:@{[UIColor whiteColor]:UITextAttributeTextColor}];     [self presentViewController:navigationController animated:YES completion:nil]; 

The navigationBar translucency gets turned off and it is dark, but the title also stays dark. I've also tried to create a custom label and set it as the title view with not much luck.

How can the title color be changed?

like image 270
Live2Enjoy7 Avatar asked Oct 15 '13 23:10

Live2Enjoy7


People also ask

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

Changing the text color 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.

How do I change the color of the navigation bar in my storyboard?

Let's see how to change the background color of a navigation bar through the storyboard editor. Create a new project, select it's view controller and embed in navigation controller. Select the navigation bar and go to It's attribute inspector.


2 Answers

The appearance api continues to evolve, and UITextAttributeTextColor is now replaced with NSForegroundColorAttributeName.

[navigationController.navigationBar setTitleTextAttributes:@{NSForegroundColorAttributeName : [UIColor whiteColor]}]; 

I'll add two things:
The key comes first, then the object.

If you want to globally change your nav controller's title attributes, use the appearance api:

[[UINavigationBar appearance] setTitleTextAttributes:@{NSForegroundColorAttributeName: [UIColor whiteColor]}]; 

I'd put that appearance api call in your app delegate's didFinishLaunchingWithOptions method.

UPDATE: Might as well post the Swift equivalents.

To update the navigationBar for an individual view controller, one can use:

self.navigationController.navigationBar.titleTextAttributes = [NSForegroundColorAttributeName:UIColor.whiteColor()] 

To change the navigation bar's appearance throughout an entire app, one can use:

UINavigationBar.appearance().titleTextAttributes = [NSForegroundColorAttributeName:UIColor.whiteColor()] 
like image 75
Mark Semsel Avatar answered Sep 20 '22 13:09

Mark Semsel


Not working for me. Using UINavigation as a modal view controller.

Apparently title colour needs to be set uiapplication level

Edit:

Best way is to change the navigation title in each VC context:

[self.navigationController.navigationBar setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:[UIColor whiteColor], NSForegroundColorAttributeName,nil]]; 
like image 28
wolffan Avatar answered Sep 16 '22 13:09

wolffan