How to change navigation bar title color? - ios

I tried to change title color like this, but it has no effect.
NSDictionary *navbarTitleTextAttributes = [NSDictionary dictionaryWithObjectsAndKeys:[UIColor whiteColor], NSForegroundColorAttributeName, nil];
[[UINavigationBar appearance] setTitleTextAttributes:navbarTitleTextAttributes];
Maybe the problem that navigationbar style is black?
self.navigationController.navigationBar.barStyle = UIBarStyleBlack;

Try this :
self.navigationItem.title = #"The title";
self.navigationController.navigationBar.titleTextAttributes = [NSDictionary dictionaryWithObject:[UIColor redColor] forKey:NSForegroundColorAttributeName];

Use UITextAttributeTextColor instead of NSForegroundColorAttributeName for the key, and it will work :)

Related

Can black navigationbar title color change?

My navigationBar is black:
self.navigationController.navigationBar.barStyle = UIBarStyleBlack;
but can not change textcolor in title, it has no effect, why?
NSDictionary *navbarTitleTextAttributes = [NSDictionary dictionaryWithObjectsAndKeys:[UIColor whiteColor], NSForegroundColorAttributeName, nil];
[[UINavigationBar appearance] setTitleTextAttributes:navbarTitleTextAttributes];
Try using this code:
NSDictionary *navbarTitleTextAttributes = [NSDictionary dictionaryWithObjectsAndKeys:
[UIColor whiteColor],UITextAttributeTextColor,
[UIColor blackColor], UITextAttributeTextShadowColor,
[NSValue valueWithUIOffset:UIOffsetMake(-1, 0)], UITextAttributeTextShadowOffset, nil];
[[UINavigationBar appearance] setTitleTextAttributes:navbarTitleTextAttributes];
Try this code.You can change your title color easily :)
self.navigationController.navigationBar.tintColor = White;
self.navigationController.navigationBarHidden=NO;
self.navigationItem.title = #"yourTitle";
[self.navigationController.navigationBar setTitleTextAttributes:
#{NSForegroundColorAttributeName:White}];
self.navigationController.navigationBar.barTintColor = [UIColor blackColor];
[[UINavigationBar appearance] setTitleTextAttributes:#{NSFontAttributeName:[UIFont fontWithName:kBoldFontName size:16],NSForegroundColorAttributeName:[UIColor blueColor]}];
It is working fine for me please try once.
I tried below code it is working fine.I am using Xcode 8 and iOS 10.2
self.navigationController.navigationBar.barStyle = UIBarStyleBlack;
self.navigationItem.title = #"your Title here";
[self.navigationController.navigationBar setTitleTextAttributes:
#{NSForegroundColorAttributeName:[UIColor greenColor]}];
self.navigationController.navigationBar.barTintColor = [UIColor lightGrayColor];

How do you change the font of the Navigation Bar?

How do you change the font of the Navigation Bar?
try like attributes with titleTextAttributes property...
let attributesDictionary = [NSFontAttributeName: UIFont(name: "your font name", size: 24)!, NSForegroundColorAttributeName : UIColor.whiteColor()]
UINavigationBar.appearance().titleTextAttributes = attributesDictionary
Try this code, this is written in Obj-C but you can get idea from here
[[UINavigationBar appearance] setTitleTextAttributes: [NSDictionary dictionaryWithObjectsAndKeys:
[UIColor colorWithRed:245.0/255.0 green:245.0/255.0 blue:245.0/255.0 alpha:1.0], NSForegroundColorAttributeName,
[UIFont fontWithName:#"HelveticaNeue-CondensedBlack" size:21.0], NSFontAttributeName, nil]];
for more options on customization you can check this nice tutorial
[self.navigationController.navigationBar setTitleTextAttributes:#{NSForegroundColorAttributeName:[UIColor redColor], NSFontAttributeName:[UIFont fontWithName:#"mplus-1c-regular" size:21]}];
If you wanna change the font of all navigation bar, you can do this in OC:
[[UINavigationBar appearance] setTitleTextAttributes:
#{NSFontAttributeName:[UIFont systemFontOfSize:22], // font size
NSForegroundColorAttributeName:[UIColor yellowColor], // foreground color
NSBackgroundColorAttributeName:[UIColor blackColor]}]; // background color
this in Swift:
UINavigationBar.appearance().titleTextAttributes =
[NSFontAttributeName:UIFont.systemFontOfSize(22), // font size
NSBackgroundColorAttributeName:UIColor.yellowColor(), // background color
NSForegroundColorAttributeName:UIColor.blackColor()] // foreground color
But if you just wanna change that of some specified view controllers, you can do this in OC:
[self.navigationController.navigationBar setTitleTextAttributes:
#{NSFontAttributeName:[UIFont systemFontOfSize:22],
NSForegroundColorAttributeName:[UIColor yellowColor],
NSBackgroundColorAttributeName:[UIColor blackColor]}];
and this in Swift:
self.navigationController?.navigationBar.titleTextAttributes =
[NSFontAttributeName:UIFont.systemFontOfSize(22),
NSForegroundColorAttributeName:UIColor.yellowColor(),
NSBackgroundColorAttributeName:UIColor.blackColor()]
Hope these can be helpful.

NavigationBar title is coming in black color

I am using a UINavigationBar in my app with UITabBar.
On the first tab, the navigation bar title is coming properly as a white title with the default background color, but on the second tab it's not working in the same way. I'm not using any code to change the title color or the navigation bar's tintColor.
First view:
http://img407.imageshack.us/img407/7192/4go.png
Second View:
Why is the second view's UINavigationBar title drawn in this black color?
Generally, you can not change default color of UINavigationBar Title. In case of If you want to change color of UINavigationBar Title than you need to customize UINavigationBar. so put code for your second ViewController for more Understanding.
EDIT:
After searching, I found that You can change title color of UINavigationBar by
self.navigationController.navigationBar.titleTextAttributes = [NSDictionary dictionaryWithObject:[UIColor whiteColor] forKey:UITextAttributeTextColor];
This code is working in iOS5 and later.
Most of the above suggestions are deprecated now, for iOS 7 use -
NSDictionary *textAttributes = [NSDictionary dictionaryWithObjectsAndKeys:
[UIColor whiteColor],NSForegroundColorAttributeName,
[UIColor whiteColor],NSBackgroundColorAttributeName,nil];
self.navigationController.navigationBar.titleTextAttributes = textAttributes;
self.title = #"Title of the Page";
Also, checkout the NSAttributedString.h for various text properties that could be set.
Try this in AppDelegate:
[[UINavigationBar appearance] setTitleTextAttributes:#{NSForegroundColorAttributeName:[UIColor whiteColor]}];
This will allow you to change the colors
NSDictionary *navbarTitleTextAttributes = [NSDictionary dictionaryWithObjectsAndKeys:
[UIColor whiteColor],UITextAttributeTextColor,
[UIColor blackColor], UITextAttributeTextShadowColor,
[NSValue valueWithUIOffset:UIOffsetMake(-1, 0)], UITextAttributeTextShadowOffset, nil];
[[UINavigationBar appearance] setTitleTextAttributes:navbarTitleTextAttributes];
Use this bit of code for iOS7 as UITextAttributeTextColor has been deprecated.
self.navigationController.navigationBar.titleTextAttributes = [NSDictionary dictionaryWithObject:[UIColor orangeColor] forKey:NSForegroundColorAttributeName];
This code changes the text of all the navigationBar, with this code the text can be customized 100%.
in appDelegate:
//custom text navBar
[[UINavigationBar appearance] setTitleTextAttributes:
[NSDictionary dictionaryWithObjectsAndKeys:[UIColor colorWithRed:0x73/255.0 green:0x47/255.0 blue:0x41/255.0 alpha:1.0], UITextAttributeTextColor,
[UIColor colorWithRed:0x1D/255.0 green:0x1D/255.0 blue:0x1B/255.0 alpha:1], UITextAttributeTextShadowColor,
[NSValue valueWithUIOffset:UIOffsetMake(0, 1)],UITextAttributeTextShadowOffset,
[UIFont fontWithName:#"yourFont" size:20], UITextAttributeFont, nil]];

Change font on iOS leftBarButtonItem?

Appearance protocol only changes font of navbar's title.
iOS 5
You have to use the appearance protocol for UIBarButtonItem for the UINavigationBar tree:
UIBarButtonItem *barButtonItemProxy = [UIBarButtonItem appearanceWhenContainedIn:
[UINavigationBar class], [UINavigationController class], nil];
NSDictionary *textAttributes = #{
UITextAttributeFont : [UIFont boldSystemFontOfSize:12.0f],
UITextAttributeTextColor : [UIColor whiteColor],
UITextAttributeTextShadowColor : [UIColor blackColor],
UITextAttributeTextShadowOffset : [NSValue valueWithUIOffset:UIOffsetMake(0.0f, -1.0f)]
};
[barButtonItemProxy setTitleTextAttributes:textAttributes forState:UIControlStateNormal];

iOS - Globally change navigation bar title color using appearance?

This crashes the app:
[[UINavigationBar appearance] setTitleColor:[UIColor darkGrayColor] forState:UIControlStateNormal];
Is there a way to do this using appearance?
This worked:
NSDictionary *textTitleOptions = [NSDictionary dictionaryWithObjectsAndKeys:[UIColor darkGrayColor], UITextAttributeTextColor, [UIColor whiteColor], UITextAttributeTextShadowColor, nil];
[[UINavigationBar appearance] setTitleTextAttributes:textTitleOptions];
Here's an example of how to do this in Swift:
UINavigationBar.appearance().titleTextAttributes =
[NSFontAttributeName:UIFont(name:"Exo2-Bold", size: 18) as! AnyObject,
NSForegroundColorAttributeName:UIColor.whiteColor()]
That crashes the app before UINavigationBar doesn't have a title or state... Those are UIButton methods
You need
[[UINavigationBar appearance] setTintColor:[UIColor darkGrayColor]];
The #RyJ answer is great and worked for me. Thought I'd chip in that there's a good tutorial on this in Ray Wenderlich's site, titled (excuse the pun):
User Interface Customization in iOS 6
See the section Customizing UINavigationBar
Here's the code snippet for the navigation bar title, to change globally:
// Customize the title text for *all* UINavigationBars
[[UINavigationBar appearance] setTitleTextAttributes:
[NSDictionary dictionaryWithObjectsAndKeys:
[UIColor colorWithRed:255.0/255.0 green:255.0/255.0 blue:255.0/255.0 alpha:1.0],
UITextAttributeTextColor,
[UIColor colorWithRed:0.0 green:0.0 blue:0.0 alpha:0.8],
UITextAttributeTextShadowColor,
[NSValue valueWithUIOffset:UIOffsetMake(0, -1)],
UITextAttributeTextShadowOffset,
[UIFont fontWithName:#"Arial-Bold" size:0.0],
UITextAttributeFont,
nil]];
One other minor point is that it seems there's a default shadow on the title bar, so to get rid of it, you can't just remove the attribute. Instead you have to set a shadow offset:
UITextAttributeTextShadowOffset : [NSValue valueWithUIOffset:UIOffsetMake(0, 0)]
for iOS 15
let appearance = UINavigationBarAppearance()
appearance.configureWithOpaqueBackground()
appearance.backgroundColor = <PREFERRED BACKGROUND COLOR>
appearance.titleTextAttributes = [.foregroundColor : <PREFERRED TITLE COLOR>]
navigationBar.tintColor = <PREFERED TINT COLOR> //for bar buttons
navigationBar.standardAppearance = appearance;
navigationBar.scrollEdgeAppearance = navigationBar.standardAppearance
I used following code to change the title bar's color.
NSShadow *shadow = [[NSShadow alloc] init];
shadow.shadowColor = [UIColor blackColor];
shadow.shadowOffset = CGSizeMake(1, 0);
NSDictionary *titleTextAttributes = #{NSForegroundColorAttributeName:[UIColor whiteColor],
NSShadowAttributeName:shadow};
[[UINavigationBar appearance] setTitleTextAttributes:titleTextAttributes];
Using modern syntax and code that actually runs, this is how to globally style your UINavigationBar title text:
NSShadow *navigationBarTitleShadow = [[NSShadow alloc] init];
navigationBarTitleShadow.shadowColor = [UIColor colorWithWhite:0.5
alpha:0.5];
navigationBarTitleShadow.shadowOffset = CGSizeMake(2.0, 2.0);
[[UINavigationBar appearance] setTitleTextAttributes:#{ NSForegroundColorAttributeName : [UIColor blackColor],
NSFontAttributeName : [UIFont fontWithName:#"Arial-BoldMT"
size:30.0],
NSShadowAttributeName : navigationBarTitleShadow }];
Note: NSShadow's shadowBlurRadius property is not respected.
Note: Shadows are so iOS 6. Don't ever use them.

Resources