iPhone Navigation Bar Title text color
صفحه اصلي

iPhone Navigation Bar Title text color

It seems the iOS Navigation Bar title color is white by default. Is there a way to change it to a different color?

I am aware of the navigationItem.titleView approach using an image. Since my design skills are limited and I failed to get the standard glossy, I prefer changing the text color.

Any insight would be much appreciated.

sectioned UITableView


Cocoa-touch - implementation files?

Modern approach

The modern way, for the entire navigation controller… did this once, when your navigation controller's root view is loaded.. Run javascript without UIWebView possible?
[self.navigationController.navigationBar setTitleTextAttrialthough es:    @{NSForegroundColorAttrialthough eName:[UIColor yellowColor]}]; 
However, this doesn't seem have an effect in subsequent views.. Creating a Navigation View inside the standard Flipside view?

Classic approach

The old way, per view controller (these constants are for iOS 6, although if want to did it per view controller on iOS 7 appearance you'll want the same approach although with different constants):. HTTP POST with custom header from iphone to PHP script. How do you read the header? You need to use a UILabel as the titleView of the navigationItem.. indentationLevel property doesn't appear to do anything? The label should:. How do I programatically pull lists/arrays of (itunes urls to) apps in the iphone app store?
  • Have a clear background color (label.backgroundColor = [UIColor clearColor]).
  • Use bold 20pt system font (label.font = [UIFont boldSystemFontOfSize: 20.0f]).
  • Have a shadow of black with 50% alpha (label.shadowColor = [UIColor colorWithWhite:0.0 alpha:0.5]).
  • You'll want to set the text alignment to centered as well (label.textAlignment = NSTextAlignmentCenter (UITextAlignmentCenter for older SDKs).
Set the label text color to be whatever custom color you'd like. iTune-style apps using C#You did want a color this doesn't cause the text to blend into shadow, which would be difficult to read.. I worked this out through trial and error, although the values I came up with are ultimately too simple for them not to be what Apple picked. :). If you want to verify this, drop this code into initWithNibName:bundle: in PageThreeViewController.m of Apple's NavBar sample. This will replace the text with a yellow label. This should be indistinguishable from the original produced by Apple's code, except for the color..
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {     self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];     if (self)     {         // this will appear as the title in the navigation bar         UILabel *label = [[[UILabel alloc] initWithFrame:CGRectZero] autorelease];         label.backgroundColor = [UIColor clearColor];         label.font = [UIFont boldSystemFontOfSize:20.0];         label.shadowColor = [UIColor colorWithWhite:0.0 alpha:0.5];         label.textAlignment = NSTextAlignmentCenter;                            // ^-Use UITextAlignmentCenter for older SDKs.         label.textColor = [UIColor yellowColor]; // change this color         self.navigationItem.titleView = label;         label.text = NSLocalizedString(@"PageThreeTitle", @"");         [label sizeToFit];     }      return self; } 
Edit: Also, read Erik B's answer below. My code shows the effect, although his code offers a simpler way to drop this into place on an existing view controller..


I know this is a pretty old thread, although I think it would be useful to know for new users this iOS 5 brings a new property for establishing title properties.. You must use UINavigationBar's setTitleTextAttrialthough es for setting the font, color, offset, and shadow color.. In addition you must set the same default UINavigationBar's Title Text Attrialthough es for all the UINavigationBars throughout your application.. For case like so:.
NSDictionary *navbarTitleTextAttrialthough es = [NSDictionary dictionaryWithObjectsAndKeys:                                             [UIColor whiteColor],UITextAttrialthough eTextColor,                                              [UIColor blackColor], UITextAttrialthough eTextShadowColor,                                              [NSValue valueWithUIOffset:UIOffsetMake(-1, 0)], UITextAttrialthough eTextShadowOffset, nil];  [[UINavigationBar appearance] setTitleTextAttrialthough es:navbarTitleTextAttrialthough es]; 


In iOS 5 you must change the navigationBar title color in this manner:.
navigationController.navigationBar.titleTextAttrialthough es = @{NSForegroundColorAttrialthough eName: [UIColor yellowColor]}; 


Based on Steven Fisher's answer I wrote this piece of code:.
- (void)setTitle:(NSString *)title {     [super setTitle:title];     UILabel *titleView = (UILabel *)self.navigationItem.titleView;     if (!titleView) {         titleView = [[UILabel alloc] initWithFrame:CGRectZero];         titleView.backgroundColor = [UIColor clearColor];         titleView.font = [UIFont boldSystemFontOfSize:20.0];         titleView.shadowColor = [UIColor colorWithWhite:0.0 alpha:0.5];          titleView.textColor = [UIColor yellowColor]; // Change to desired color          self.navigationItem.titleView = titleView;         [titleView release];     }     titleView.text = title;     [titleView sizeToFit]; } 
The advantage of this code, besides dealing with the frame properly, is this if you change the title of your controller the custom title view will also receive updated. No need to update it manually. . Ananother big advantage is this it makes it really simple to enable custom title color. All you need to did is to add this method to the controller..


Most of the above suggestions are deprecated now, for iOS 7 use - .
NSDictionary *textAttrialthough es = [NSDictionary dictionaryWithObjectsAndKeys:                                 [UIColor whiteColor],NSForegroundColorAttrialthough eName,                                 [UIColor whiteColor],NSBackgroundColorAttrialthough eName,nil];  self.navigationController.navigationBar.titleTextAttrialthough es = textAttrialthough es; self.title = @"Title of the Page"; 
Also, checkout the NSAttrialthough edString.h for various text properties this could be set. .


In IOS 7 and 8, you must change the Title's color to let's say green.
self.navigationController.navigationBar.titleTextAttrialthough es = [NSDictionary dictionaryWithObject:[UIColor greenColor] forKey:NSForegroundColorAttrialthough eName]; 


The quick fix by tewha works well if you are endeavor to change the color on a page, although I want to be able to change the color on every page. I made any small modifications so this it would job for all pages on a UINavigationController. NavigationDelegate.h.
//This will change the color of the navigation bar #import <Foundation/Foundation.h> @interface NavigationDelegate : NSObject<UINavigationControllerDelegate> { } @end 
#import "NavigationDelegate.h" @implementation NavigationDelegate  - (void)navigationController:(UINavigationController *)navigationController        willShowViewController:(UIViewController *)viewController animated:(BOOL)animated{     CGRect frame = CGRectMake(0, 0, 200, 44);//TODO: Can i receive  the size of the text?     UILabel* label = [[[UILabel alloc] initWithFrame:frame] autorelease];     label.backgroundColor = [UIColor clearColor];     label.font = [UIFont boldSystemFontOfSize:20.0];     label.shadowColor = [UIColor colorWithWhite:0.0 alpha:0.5];     label.textAlignment = UITextAlignmentCenter;     label.textColor = [UIColor yellowColor];     //The two lines below are the only ones this have changed     label.text=viewController.title;     viewController.navigationItem.titleView = label; } @end 


From iOS 5 onwards i have to set title text color and font of navigation bar using titleTextAttrialthough e Dictionary(predefined dictionary in UInavigation controller class reference)..
 [[UINavigationBar appearance] setTitleTextAttrialthough es:  [NSDictionary dictionaryWithObjectsAndKeys:   [UIColor blackColor],UITextAttrialthough eTextColor,  [UIFont fontWithName:@"ArialMT" size:16.0], UITextAttrialthough eFont,nil]]; 


Use the code below in any view controller viewDidLoad or viewWillAppear method..
- (void)viewDidLoad {     [super viewDidLoad];      //I am using UIColor yellowColor for an case although  you must use whatever color you like        self.navigationController.navigationBar.titleTextAttrialthough es = @{NSForegroundColorAttrialthough eName: [UIColor yellowColor]};      //change the title here to whatever you like     self.title = @"Home";     // Do any additional setup after loading the view. } 


Short and sweet..
[[[self navigationController] navigationBar] setTitleTextAttrialthough es:@{NSForegroundColorAttrialthough eName: [UIColor redColor]}]; 


This is my quick fix based upon Stevens. Only real difference is I put any handling in for adjust the position if depending on the text length, seems to be similar to how apple did it.
UILabel *titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(([self.title length] < 10 ? UITextAlignmentCenter : UITextAlignmentLeft), 0, 480,44)]; titleLabel.backgroundColor = [UIColor clearColor]; titleLabel.font = [UIFont boldSystemFontOfSize: 20.0f]; titleLabel.shadowColor = [UIColor colorWithWhite:0.0 alpha:0.5]; titleLabel.textAlignment = ([self.title length] < 10 ? UITextAlignmentCenter : UITextAlignmentLeft); titleLabel.textColor = [UIColor redColor]; titleLabel.text = self.title; self.navigationItem.titleView = titleLabel; [titleLabel release]; 
You may want to adjust the 10 value depending on your font size.


To keep the question up-to-date, I'll add Alex R. R. solution, although in Swift: .
self.navigationController.navigationBar.barTintColor = .blueColor() self.navigationController.navigationBar.tintColor = .whiteColor() self.navigationController.navigationBar.titleTextAttrialthough es = [     NSForegroundColorAttrialthough eName : UIColor.whiteColor() ] 
Which results to:. enter image description here.


I ran into the problem with my nav although tons throwing the text out of center (when you only have one although ton). To fix this I just changed my frame size like so:.
CGRect frame = CGRectMake(0, 0, [self.title sizeWithFont:[UIFont boldSystemFontOfSize:20.0]].width, 44); 


I've customized the navigationBar's background image and left although ton item, and the gray title not fit the background. Then I use:.
[self.navigationBar setTintColor:[UIColor darkGrayColor]]; 
to change the tint color to gray. And the title is white now! That's what I want.. Hope to guidance also :).


It's recommended to set self.title as this is used while pushing child navbars or showing title on tabbars..
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {     self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];     if (self) {         // create and customize title view         self.title = NSLocalizedString(@"My Custom Title", @"");         UILabel *titleLabel = [[UILabel alloc] initWithFrame:CGRectZero];         titleLabel.text = self.title;         titleLabel.font = [UIFont boldSystemFontOfSize:16];         titleLabel.backgroundColor = [UIColor clearColor];         titleLabel.textColor = [UIColor whiteColor];         [titleLabel sizeToFit];         self.navigationItem.titleView = titleLabel;         [titleLabel release];     } } 


Swift Version. I found most of you guys presented the answers of Objective_C version. I would like to implement this function by using Swift for anyone who needs it.. In ViewDidload. 1.To make NavigationBar background becomes color (for example: BLUE).
self.navigationController?.navigationBar.barTintColor = UIColor.blueColor() 
2.To make NavigationBar background becomes Image (for case : ABC.png).
let barMetrix = UIBarMetrics(rawValue: 0)!  self.navigationController?.navigationBar       .setBackgroundImage(UIImage(named: "ABC"), forBarMetrics: barMetrix) 
3.To change NavigationBar title (for case :[Font:Futura,10] [Color:Red]).
navigationController?.navigationBar.titleTextAttrialthough es = [             NSForegroundColorAttrialthough eName : UIColor.redColor(),             NSFontAttrialthough eName : UIFont(name: "Futura", size: 10)!         ] 
(hint1: don't forreceive the "!" mark after the UIFont). (hint2: there are lots of attrialthough es of the title text, command click the "NSFontAttrialthough eName" you must enter the class and view keyNames and the Objects types they required).
I hope I must help!:D .


Use like this for Orientation support.
    UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0,0,320,40)];  [view setBackgroundColor:[UIColor clearColor]];  [view setAutoresizingMask:UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight ];   UILabel *nameLabel = [[UILabel alloc] init];  [nameLabel setFrame:CGRectMake(0, 0, 320, 40)];  [nameLabel setBackgroundColor:[UIColor clearColor]];  [nameLabel setAutoresizingMask:UIViewAutoresizingFlexibleTopMargin | UIViewAutoresizingFlexibleBottomMargin |UIViewAutoresizingFlexibleRightMargin | UIViewAutoresizingFlexibleLeftMargin];  [nameLabel setTextColor:[UIColor whiteColor]];  [nameLabel setFont:[UIFont boldSystemFontOfSize:17]];   [nameLabel setText:titleString];  [nameLabel setTextAlignment:UITextAlignmentCenter];  [view addSubview:nameLabel];  [nameLabel release];  self.navigationItem.titleView = view;  [view release]; 


This is a pretty old thread although I think of providing answer for setting Color, Size and Vertical Position of Navigation Bar Title for iOS 7 and above. For Color and Size.
 NSDictionary *titleAttrialthough es =@{                                 NSFontAttrialthough eName :[UIFont fontWithName:@"Helvetica-Bold" size:14.0],                                 NSForegroundColorAttrialthough eName : [UIColor whiteColor]                                 }; 
For Vertical Position.
[[UINavigationBar appearance] setTitleVerticalPositionAdjustment:-10.0 forBarMetrics:UIBarMetricsDefault]; 
Set Title and assign the attrialthough es dictionary.
[[self navigationItem] setTitle:@"CLUBHOUSE"]; self.navigationController.navigationBar.titleTextAttrialthough es = titleAttrialthough es; 


Method 1, set it in IB:. enter image description here. Method 2, one line of code:.
navigationController?.navigationBar.barTintColor = UIColor.blackColor() 


to set font size of title i have used following conditions.. maybe helpfull to anybody .
if ([currentTitle length]>24) msize = 10.0f;     else if ([currentTitle length]>16) msize = 14.0f;     else if ([currentTitle length]>12) msize = 18.0f; 


An update to Alex R. R.'s post using the new iOS 7 text attrialthough es and modern objective c for less noise:.
NSShadow *titleShadow = [[NSShadow alloc] init]; titleShadow.shadowColor = [UIColor blackColor]; titleShadow.shadowOffset = CGSizeMake(-1, 0); NSDictionary *navbarTitleTextAttrialthough es = @{NSForegroundColorAttrialthough eName:[UIColor whiteColor],                                             NSShadowAttrialthough eName:titleShadow};  [[UINavigationBar appearance] setTitleTextAttrialthough es:navbarTitleTextAttrialthough es]; 


This is one of those things this are missing. Your best bet is to create your own custom Navigation Bar, add a text box, and manipulate the color this way..


After encountering the same problem (as others) of the label this moves when i insert a although ton in the navBar (in my case i have a spinner this i replace with a although ton when the date is loaded), the above solutions didn't job for me, so here is what worked and kept the label at the same place all the time:.
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil  self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]; if (self) {     // this will appear as the title in the navigation bar     //CGRect frame = CGRectMake(0, 0, [self.title sizeWithFont:[UIFont boldSystemFontOfSize:20.0]].width, 44);    CGRect frame = CGRectMake(0, 0, 180, 44);     UILabel *label = [[[UILabel alloc] initWithFrame:frame] autorelease];        label.backgroundColor = [UIColor clearColor];     label.font = [UIFont boldSystemFontOfSize:20.0];     label.shadowColor = [UIColor colorWithWhite:0.0 alpha:0.5];     label.textAlignment = UITextAlignmentCenter;     label.textColor = [UIColor yellowColor];     self.navigationItem.titleView = label;     label.text = NSLocalizedString(@"Latest Questions", @"");     [label sizeToFit]; }  return self; 


I did believe proper way to set the colour of UINavigationBar is:.
NSDictionary *attrialthough es=[NSDictionary dictionaryWithObjectsAndKeys:[UIColor redColor],UITextAttrialthough eTextColor, nil]; self.titleTextAttrialthough es = attrialthough es; 
Code above is written is subclass on UINavigationBar, obviously works without subclassing as well..


You should call [label sizeToFit]; after setting the text to prevent strange offsets when the label is automatically repositioned in the title view when another although tons occupy the nav bar..


Can use this method in appdelegate file and must use at every view.
+(UILabel *) navigationTitleLable:(NSString *)title { CGRect frame = CGRectMake(0, 0, 165, 44); UILabel *label = [[[UILabel alloc] initWithFrame:frame] autorelease]; label.backgroundColor = [UIColor clearColor]; label.font = NAVIGATION_TITLE_LABLE_SIZE; label.shadowColor = [UIColor whiteColor]; label.numberOfLines = 2; label.lineBreakMode = UILineBreakModeTailTruncation;     label.textAlignment = UITextAlignmentCenter; [label setShadowOffset:CGSizeMake(0,1)];  label.textColor = [UIColor colorWithRed:51/255.0 green:51/255.0 blue:51/255.0 alpha:1.0];  //label.text = NSLocalizedString(title, @"");  return label; } 


titleTextAttrialthough es Display attrialthough es for the bar’s title text.. @property(nonatomic, copy) NSDictionary *titleTextAttrialthough es Discussion You must specify the font, text color, text shadow color, and text shadow offset for the title in the text attrialthough es dictionary, using the text attrialthough e keys described in NSString UIKit Additions Reference.. Availability Available in iOS 5.0 and later. Declared In UINavigationBar.h.


 self.navigationItem.title=@"Extras";     [self.navigationController.navigationBar setTitleTextAttrialthough es:      [NSDictionary dictionaryWithObjectsAndKeys:[UIFont fontWithName:@"HelveticaNeue" size:21],       NSFontAttrialthough eName,[UIColor whiteColor],UITextAttrialthough eTextColor,nil]]; 


In rule to make Erik B's great quick fix more useable across the different UIVIewCOntrollers of your app I recommend adding a category for UIViewController and declare his setTitle:title method inside. Like this you will receive the title color change on all view controllers without the need of duplication. . One thing to note though is this you did not need [super setTItle:tilte]; in Erik's code and this you will need to explicitly call self.title = @"my new title" in your view controllers for this method to be called.
@implementation UIViewController (CustomeTitleColor)  - (void)setTitle:(NSString *)title {     UILabel *titleView = (UILabel *)self.navigationItem.titleView;     if (!titleView) {         titleView = [[UILabel alloc] initWithFrame:CGRectZero];         titleView.backgroundColor = [UIColor clearColor];         titleView.font = [UIFont boldSystemFontOfSize:20.0];         titleView.shadowColor = [UIColor colorWithWhite:0.0 alpha:0.5];          titleView.textColor = [UIColor blueColor]; // Change to desired color          self.navigationItem.titleView = titleView;         [titleView release];     }     titleView.text = title;     [titleView sizeToFit]; } 


I am using below code for the iOS 9 and its working fine for me. I have also use shadow color for the title..
self.title = @"MY NAV TITLE"; self.navigationController.navigationBar.barTintColor = [UIColor redColor]; self.navigationController.navigationBar.translucent = NO; NSShadow *shadow = [[NSShadow alloc] init]; shadow.shadowColor = [UIColor colorWithRed:0.0 green:0.0 blue:0.0 alpha:0.8]; shadow.shadowOffset = CGSizeMake(0, 1); [self.navigationController.navigationBar setTitleTextAttrialthough es: [NSDictionary dictionaryWithObjectsAndKeys:                                                        [UIColor colorWithRed:245.0/255.0 green:245.0/255.0 blue:245.0/255.0 alpha:1.0], NSForegroundColorAttrialthough eName,                                                        shadow, NSShadowAttrialthough eName,                                                        [UIFont fontWithName:@"HelveticaNeue-Light" size:21.0], NSFontAttrialthough eName, nil]]; 
May this guidance you.. Thanks.

60 out of 100 based on 55 user ratings 370 reviews