Change in UINavigationBar's color - ios

I am working on iOS 7 app. My navigation bar used to look as in the image below:
but after adding this piece of code
self.edgesForExtendedLayout = UIRectEdgeNone;
The navigation car color got darker as in the next image:.
How do we let the navigation bar to stay brighter as in the first image while keeping the code above?

By default, the translucent property of navigation bar is set to YES.
Additionally, there is a system blur applied to all navigation bars. Under this setting, iOS 7 tends to desaturate the color of the bar.
Difference Translucent settings
Setting Tint Color
Turn off translucent setting
Put this code in appDelegate.m in didFinishLaunchingWithOptions:
#define UIColorFromRGB(rgbValue) [UIColor colorWithRed:((float)((rgbValue & 0xFF0000) >> 16))/255.0 green:((float)((rgbValue & 0xFF00) >> 8))/255.0 blue:((float)(rgbValue & 0xFF))/255.0 alpha:1.0]
if (floor(NSFoundationVersionNumber) >= NSFoundationVersionNumber_iOS_6_1)
{
// Load resources for iOS 7 or later
// To change the background color of navigation bar
[[UINavigationBar appearance] setBarTintColor:UIColorFromRGB(0x067AB5)];
// To change the color of back button
[[UINavigationBar appearance] setTintColor:[UIColor whiteColor]];
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);
[[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,
shadow, NSShadowAttributeName,
[UIFont fontWithName:#"HelveticaNeue-CondensedBlack" size:21.0], NSFontAttributeName, nil]];
}

Try this out
For iOS7, Navigation bar's color can changed by this few lines.
if(IS_IOS7){
//Your color code
self.navigationController.navigationBar.barTintColor = [UIColor colorWithRed:34.0/255.0 green:59.0/255.0 blue:135.0/255.0 alpha:1.0];
self.navigationController.navigationBar.translucent = NO;
self.navigationController.navigationBar.titleTextAttributes
= #{UITextAttributeTextColor : [UIColor whiteColor]};
[self.navigationController.navigationBar setTintColor:[UIColor whiteColor]];
self.navigationController.navigationBar.barStyle = UIBarStyleBlackTranslucent;
}

In iOS 7, a navigation bar’s tintColor affects the color of the back indicator image, button titles, and button images. The barTintColor property affects the color of the bar itself. Additionally, navigation bars are translucent by default. Turning the translucency off or on does not affect buttons, since they do not have backgrounds.
add this code in your appdelegate
if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 7)
{
[[UINavigationBar appearance] setBarTintColor:[UIColor yourColorCode]];
//optional
NSShadow *shadowObj = [[NSShadow alloc] init];
shadowObj.shadowColor = [UIColor colorWithRed:0.0 green:0.0 blue:0.0 alpha:0.8];
shadowObj.shadowOffset = CGSizeMake(0, 1);
[[UINavigationBar appearance] setTitleTextAttributes: [NSDictionary dictionaryWithObjectsAndKeys:
[UIColor colorWithRed:205.0/255.0 green:255.0/255.0 blue:45.0/255.0 alpha:1.0], NSForegroundColorAttributeName,
shadowObj, NSShadowAttributeName,
[UIFont fontWithName:#"Arial" size:18.0], NSFontAttributeName, nil]];
[[UINavigationBar appearance] setTintColor:[UIColor blackColor]];
}

Try this Code
navigationController.navigationBar.tintColor = [UIColor colorWithRed:117/255.0f green:4/255.0f blue:32/255.0f alpha:1];

Related

UINavigationBar back button item font color won't set

I have a navigation bar on a viewController that I can enable/disable. The problem is I can't get the font for the UIBarButtonItems to change colors after the view initially loads, though the back arrow will change.
I disable the UINavigationBar on myViewController with the following line of code:
self.navigationController.navigationBar.userInteractionEnabled = NO;
I have UIControlStates configured in AppDelegate.m for UIBarButtonItems for enabled and disabled, but nothing happens to the font after the view initially loads.
In my AppDelegate.m, I have the following code to set the UINavigationBar's font and color:
// UIBarButtonItem styling
NSShadow *shadow = [[NSShadow alloc]init];
shadow.shadowOffset = CGSizeMake(0.0, 1.0);
shadow.shadowColor = [UIColor whiteColor];
// enabled
NSDictionary *enabledTextAttributeDictionary = #{NSForegroundColorAttributeName : [UIColor customCellHyperlinkColor], NSShadowAttributeName: shadow, NSFontAttributeName:[UIFont fontWithName:#"GillSans" size:17.0]};
[[UIBarButtonItem appearanceWhenContainedIn:[UINavigationBar class], nil] setTitleTextAttributes:enabledTextAttributeDictionary forState:UIControlStateNormal];
// disabled
NSDictionary *disabledTextAttributeDictionary = #{NSForegroundColorAttributeName : [UIColor lightGrayColor], NSShadowAttributeName: shadow, NSFontAttributeName:[UIFont fontWithName:#"GillSans" size:17.0]};
[[UIBarButtonItem appearanceWhenContainedIn:[UINavigationBar class], nil] setTitleTextAttributes:disabledTextAttributeDictionary forState:UIControlStateDisabled];
// UINavigationBarTitle styling
NSDictionary *titleAttributeDictionary = #{NSForegroundColorAttributeName : [UIColor blackColor], NSShadowAttributeName: shadow, NSFontAttributeName:[UIFont fontWithName:#"GillSans" size:19.0]};
[[UINavigationBar appearanceWhenContainedIn:[UINavigationController class], nil]setTitleTextAttributes:titleAttributeDictionary];
I thought since I configured the 2 states (UIControlStateNormal/UIControlStateDisabled) in AppDelegate.m, nothing needs to be done other than enable/disable the navigationBar. Enable/disable does indeed enable/disable, but the color on the back button label doesn't change (though it does initially set to the color I set for UIControlStateNormal in AppDelegate).
I tried to manually set it, but the label on the backButtonItem stays blue while the icon to the left of it tints light gray. What (obvious thing) am I missing that is preventing me from changing the color of my back button font?
I think you are looking for this
Add this code in you AppDelegate.m
NSShadow *shadow = [[NSShadow alloc] init];
shadow.shadowOffset = CGSizeMake(0.0, 1.0);
shadow.shadowColor = [UIColor clearColor];
[[UIBarButtonItem appearanceWhenContainedIn:[UINavigationBar class], nil]
setTitleTextAttributes:
#{NSForegroundColorAttributeName:[UIColor redColor],
NSShadowAttributeName:shadow,
NSFontAttributeName:[UIFont systemFontOfSize:13.0]
}
forState:UIControlStateNormal];
I think you need to use
[self.navigationController.navigationBar setBarTintColor:[UIColor lightGrayColor]];
self.navigationController.navigationBar.tintColor = [UIColor lightGrayColor];
[self.navigationController.navigationBar setTitleTextAttributes:#{NSForegroundColorAttributeName : [UIColor lightGrayColor], NSFontAttributeName : place your font here}];
instead of
self.navigationController.navigationBar.tintColor = [UIColor lightGrayColor];

How to change the background color of navigationbar in ios6 and ios7 both?

How to change the background color of navigationbar in iOS6 and iOS7 both?i want to know when to use setBarTintColor: method and when to use backgroundColor to change the background color of navigationbar.
Please tell me the difference between these two methods.
And a way to change the background color of navigationbar in both ios6 and ios7.
Thanks!!
self.navigationBar.barTintColor = [UIColor blueColor];
self.navigationBar.tintColor = [UIColor whiteColor];
self.navigationBar.translucent = NO;
// barTintColor sets the background color
// tintColor sets the buttons color
in ios6
[[UINavigationBar appearance] setBackgroundColor:[UIColor redColor]];
in ios7
navigationController.navigationBar.barTintColor = [UIColor greenColor];
or
[UINavigationBar appearance] setBarTintColor:[UIColor greenColor]];
try this...i have refered this link it supports for both iOS6 and iOS7
// Uncomment to change the background color of navigation bar
[[UINavigationBar appearance] setBarTintColor:UIColorFromRGB(0x067AB5)];
// Uncomment to change the color of back button
[[UINavigationBar appearance] setTintColor:[UIColor whiteColor]];
// Uncomment to assign a custom backgroung image
[[UINavigationBar appearance] setBackgroundImage:[UIImage imageNamed:#"nav_bg_ios7.png"] forBarMetrics:UIBarMetricsDefault];
// Uncomment to change the back indicator image
[[UINavigationBar appearance] setBackIndicatorImage:[UIImage imageNamed:#"back_btn.png"]];
[[UINavigationBar appearance] setBackIndicatorTransitionMaskImage:[UIImage imageNamed:#"back_btn.png"]];
// Uncomment to change the font style of the title
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);
[[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,
shadow, NSShadowAttributeName,
[UIFont fontWithName:#"HelveticaNeue-CondensedBlack" size:21.0], NSFontAttributeName, nil]];
you can use
NSArray *ver = [[UIDevice currentDevice].systemVersion componentsSeparatedByString:#"."];
if ([[ver objectAtIndex:0] intValue] >= 7) {
// iOS 7.0 or later
self.navigationController.navigationBar.barTintColor = [UIColor redColor];
self.navigationController.navigationBar.translucent = NO;
}else {
// iOS 6.1 or earlier
self.navigationController.navigationBar.tintColor = [UIColor redColor];
}
or
if (floor(NSFoundationVersionNumber) <= NSFoundationVersionNumber_iOS_6_1) {
// iOS 6.1 or earlier
self.navigationController.navigationBar.tintColor = [UIColor redColor];
} else {
// iOS 7.0 or later
self.navigationController.navigationBar.barTintColor = [UIColor redColor];
self.navigationController.navigationBar.translucent = NO;
}
}
barTintColor = Applies to the navigation bar background. This one is only available for iOS 7. For iOS 6, you can use the tintColor.
tintColor = Applies to the navigation items and bar button items.
Developer Reference

How to change these colors on Navbar?

I have succesfully change the navbar button with this:
[[UINavigationBar appearance] setBarTintColor:[UIColor colorWithRed:26.0/255 green:132.0/255 blue:182.0/255 alpha:.5]];
What about if I want to change the color of the title (from black) or the color of the button?
This is what I have in my App Delegate in a method specially for changing appearances.
// set button tint colour to white
[[UINavigationBar appearance] setTintColor:[UIColor whiteColor]];
// set the bar tint colour to purplish
[[UINavigationBar appearance] setBarTintColor:[UIColor colorWithRed:.2 green:.458823529 blue:.592156863 alpha:1.0]];
// set title text label colour to white
[[UINavigationBar appearance] setTitleTextAttributes:#{NSForegroundColorAttributeName : [UIColor whiteColor]}];
For navigation bar title color in iOS7 you should use the setTitleTextAttributes of UINavigationBar appearance
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);
[[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,shadow, NSShadowAttributeName, [UIFont fontWithName:#"HelveticaNeue-CondensedBlack" size:21.0], NSFontAttributeName, nil]];
For more navbar customization refer http://www.appcoda.com/customize-navigation-status-bar-ios-7/

navigationBar.barTintColor always black and not possible to change it

The navigationBar.barTintColor in my app is always black, and there is no way I can change it. I checked all classes and I never set it to black, but I do set it to UIColor clearColor. Still, the bar is black. Any suggestions?
Edit:I found out that the problem is with my [UIColor clearColor], when I change it to any other color it changes the color like it should, but clearColor makes it appear black.
Have a look there
Try modifying the Style and Translucent attributes on the navigation bar (top right in image).
If you are having problems modifying the status bar color, try adding this to your .plist (line below).
<key>UIViewControllerBasedStatusBarAppearance</key>
<false/>
Finally, here's some code you may want.
// Status bar color
[[UIApplication sharedApplication] setStatusBarStyle:yourStyle];
// Navigation bars color
[UINavigationBar appearance].barStyle = yourStyle;
[UINavigationBar appearance].barTintColor = [UIColor yourColor];
// Navigation bars items color
[UINavigationBar appearance].tintColor = [UIColor yourColor];
If its IOS7 try the code below
[[UINavigationBar appearance]setBarTintColor: [<Specify the UIColor you want>];
In IOS6 try this
[[UINavigationBar appearance] setTintColor: [<Specify the UIColor you want>];
Edit:
I think you have given
self.navigationController.navigationBar.barTintColor = [UIColor clearColor];
This will give black color. If you want any specific tint color, it must be specifed after clearing
self.navigationController.navigationBar.barTintColor = [UIColor clearColor];
self.navigationController.navigationBar.barTintColor = [UIColor <specify your color>];
if([[[UIDevice currentDevice] systemVersion] floatValue] < 7.0)
{
[[UINavigationBar appearance] setBackgroundImage:[UIImage imageNamed:#"nav_bg.png"] forBarMetrics:UIBarMetricsDefault];
[[UINavigationBar appearance] setTitleTextAttributes:
#{
UITextAttributeTextColor: [UIColor whiteColor],UITextAttributeTextShadowColor: [UIColor clearColor],UITextAttributeTextShadowOffset: [NSValue valueWithUIOffset:UIOffsetMake(0.0f, 1.0f)],UITextAttributeFont: [UIFont fontWithName:#"ArialMT" size:18.0f]
}];
CGFloat verticalOffset = -4;
[[UINavigationBar appearance] setTitleVerticalPositionAdjustment:verticalOffset forBarMetrics:UIBarMetricsDefault];
}
else
{
[[UINavigationBar appearance] setBarTintColor:[UIColor whiteColor]];
// Uncomment to change the color of back button
[[UINavigationBar appearance] setTintColor:[UIColor whiteColor]];
// Uncomment to assign a custom backgroung image
[[UINavigationBar appearance] setBackgroundImage:[UIImage imageNamed:#"nav_bg_ios7.png"] forBarMetrics:UIBarMetricsDefault];
// Uncomment to change the back indicator image
[[UINavigationBar appearance] setBackIndicatorImage:[UIImage imageNamed:#""]];
[[UINavigationBar appearance] setBackIndicatorTransitionMaskImage:[UIImage imageNamed:#""]];
// Uncomment to change the font style of the title
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);
[[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,shadow, NSShadowAttributeName,[UIFont fontWithName:#"ArialMT" size:18.0], NSFontAttributeName, nil]];
CGFloat verticalOffset = -4;
[[UINavigationBar appearance] setTitleVerticalPositionAdjustment:verticalOffset forBarMetrics:UIBarMetricsDefault];
}
In iOS 7 try:
[self.navigationController.navigationBar setTranslucent:NO];

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