Timothy Rajan
Timothy Rajan

Reputation: 1957

Unable to set the colour in UINavigation bar - iOS

I am trying to set a different colour to the UINavigation bar in the iPhone app. My app is running in iOS 7 and the code snippet which I use is below

NSArray *ver = [[UIDevice currentDevice].systemVersion componentsSeparatedByString:@"."];
if ([[ver objectAtIndex:0] intValue] >= 7) {
    self.navigationController.navigationBar.barTintColor = [UIColor redColor];
    self.navigationController.navigationBar.translucent = NO;
}else {
    self.navigationController.navigationBar.tintColor = [UIColor redColor];
}

But the above code is not changing the color of the navigation bar to red. Am I missing something. Can you please help me. Thanks

Upvotes: 0

Views: 186

Answers (3)

Popeye
Popeye

Reputation: 12123

I would say it's failing the if statement if ([[ver objectAtIndex:0] intValue] >= 7). I would replace that whole if statement with:

if([[[UIDevice currentDevice] systemVersion] intValue] >= 7) {
    // Do the below if you want to set the bar tint color for every UINavigationBar
    // [[UINavigationBar appearance] setBarTintColor:[UIColor redColor]];
    // [[UINavigationBar appearance] setTranslucent:NO]; 
    [[[self navigationController] navigationBar] setBarTintColor:[UIColor redColor]];
    [[[self navigationController] navigationBar] setTranslucent:NO];

 } else {
     // Do the below if you want to set the bar tint color for every UINavigationBar
     // [[UINavigationBar appearance] setTintColor:[UIColor redColor]];
     [[[self navigationController] navigationBar] setTintColor:[UIColor redColor]];
 }

[[UIDevice currentDevice] systemVersion]; will return a NSString * that could be anything from 7 to 7.0.1. Doing floatValue will return at least 7.0 as it will append the 0 on the end if it just returns 7 so we want it to always only give us the initial system version of 7 so we need to do intValue on the end so it looks like [[[UIDevice currentDevice] systemVersion] intValue]; forget about the componentSeperatedByString: method no need at all for it, it will cause more issues then you want.

You could also do this sort of if statement

if (floor(NSFoundationVersionNumber) >= NSFoundationVersionNumber_iOS_7_0) {
    // Everything above and including iOS 7.0
} else {
    // Everything below iOS 7.0
}

Upvotes: 0

Yohan
Yohan

Reputation: 1108

For adding Color for all navigation bar default set below code in appdelegate.m

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    // Override point for customization after application launch.
    [[UINavigationBar appearance] setBarTintColor:[UIColor yellowColor]];
    return YES;
}

or else want to set color for particular view means then use this

- (void)viewDidLoad
{
    [super viewDidLoad];

   self.navigationController.navigationBar.barTintColor = [UIColor grayColor];

}

If you want learn more try this link it is good check this link

Upvotes: 1

manujmv
manujmv

Reputation: 6445

Place the below code in didFinishLaunchingWithOptions in your AppDelegate

if ([self checkOSVersion] >= 7) {
    [[UINavigationBar appearance] setBarTintColor:[UIColor redColor]];
    [[UINavigationBar appearance] setTranslucent:NO];
} else {
    [[UINavigationBar appearance] setTintColor:[UIColor redColor]];
}

- (int)checkOSVersion {

    NSArray *ver = [[[UIDevice currentDevice] systemVersion] componentsSeparatedByString:@"."];
    int osVerson = [[ver objectAtIndex:0] intValue];
    return osVerson;
}

Upvotes: 3

Related Questions