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

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

Related

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];

Change in UINavigationBar's color

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];

iOS 7 UINavigationBar appearance not working first time…

I am trying to change the look of the UINavigationBar in my iOS7 app. I am doing the following:
- (void)viewDidLoad
{
[super viewDidLoad];
m_sNumberToCall = #"";
UIBarButtonItem * btn = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:#"IconHome.png"] style:UIBarButtonItemStyleBordered target:self action:#selector(btHomeTouched:)];
self.navigationItem.leftBarButtonItem = btn;
self.navigationController.navigationBar.translucent = YES;
[[UINavigationBar appearance] setBackgroundImage:[UIImage imageNamed:#"TVCNavBack.png"] forBarMetrics:UIBarMetricsDefault];
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:#"Helvetica-Bold" size:21.0],
NSFontAttributeName,
nil]];
}
But, the first time I present the UITableViewController it is the standard iOS7 nav bar, then I press home and present it again and it is my new look.
Any ideas why it does not work the first time?
Don't change the appearance but the navigation bar directly. The appearance affects only the future instances but not the already created ones.
Change:
[[UINavigationBar appearance] setBackgroundImage:[UIImage imageNamed:#"TVCNavBack.png"] forBarMetrics:UIBarMetricsDefault];
to:
[self.navigationController.navigationBar setBackgroundImage:[UIImage imageNamed:#"TVCNavBack.png"] forBarMetrics:UIBarMetricsDefault];
The answer before only helps you with the background image but not with the title text attributes.
You don't need to change your code but all you have to do is move it to
applicationDidFinishLaunchingWithOptions
in your AppDelegate.m file.

Change navigation bar tint color iOS 7.

I know how to change navigation bat tint colour in iOS 6:
[UINavigationBar appearance].tintColor = [UIColor colorWithRed:129/255.0 green:200/255.0 blue:244/255.0 alpha:1.0];
I'm adding this code in APPDelegate page.
Now I want to do this in iOS 7 but above code is not working.
I searched on net. I got a solution. By adding below function to every page I can change navigation color.
self.navigationController.navigationBar.barTintColor = [UIColor colorWithRed:129/255.0 green:200/255.0 blue:244/255.0 alpha:1.0];
But I need a function which can add to APPDelegate function.
Please help me to overcome this issue.
Why not to use setBarTintColor for appearance proxy, you can do this:
if (floor(NSFoundationVersionNumber) <= NSFoundationVersionNumber_iOS_6_1)
{
[[UINavigationBar appearance] setTintColor: [UIColor colorWithRed:129/255.0 green:200/255.0 blue:244/255.0 alpha:1.0]];
}
else
{
[[UINavigationBar appearance] setBarTintColor: [UIColor colorWithRed:129/255.0 green:200/255.0 blue:244/255.0 alpha:1.0]];
}
you can add bellow code in appdelegate.m
if your app is navigation based
// for background color
[nav.navigationBar setBarTintColor:[UIColor blueColor]];
// for change navigation title and button color
[[UINavigationBar appearance] setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:[UIColor whiteColor], NSForegroundColorAttributeName, [UIFont fontWithName:#"FontNAme" size:20], NSFontAttributeName, nil]];
[[UINavigationBar appearance] setTintColor:[UIColor whiteColor]];
Using respondsToSelector for version checking may be better.
if ([self.navigationBar respondsToSelector:#selector(setBarTintColor:)]) {
[self.navigationBar setBarTintColor: [UIColor colorWithRed:129/255.0 green:200/255.0 blue:244/255.0 alpha:1.0]];
} else {
[self.navigationBar setTintColor: [UIColor colorWithRed:129/255.0 green:200/255.0 blue:244/255.0 alpha:1.0]];
}
In Swift, for me, I wanted to change tint color for the Cancel and Send buttons, when the e-mail pops up. And it worked great.
(UIBarButtonItem.appearanceWhenContainedInInstancesOfClasses([UINavigationBar.self])).tintColor = UIColor.whiteColor()
Try [self.navigationController.navigationBar setTranslucent:NO];

Resources