orthehelper
orthehelper

Reputation: 4079

how to set navigation bar color on IOS 6/7

i am setting my UINAvigatoinBar color like so with hex color:

self.navigationController.navigationBar.barTintColor = UIColorFromRGB(0x212121);

it works well on IOS7 but in lower versions it crash with the following :

[UINavigationBar setBarTintColor:]: unrecognized selector sent to instance

how can i do it right?

Upvotes: 1

Views: 7798

Answers (6)

Lisarien
Lisarien

Reputation: 1136

I assume the best way is to be using the respondToSelector method instead of checking the iOS version:

if ([self.navigationController.navigationBar respondsToSelector:@selector(setBarTintColor:)]) {
    [self.navigationController.navigationBar setBarTintColor:NAVBAR_BACKGROUNDCOLOR];
}
else {
    [self.navigationController.navigationBar setTintColor:NAVBAR_BACKGROUNDCOLOR];
}

Upvotes: 5

Muralikrishna
Muralikrishna

Reputation: 1044

self.navigationController.navigationBar.tintColor=[UIColor colorWithRed:(45/255.f) green:(45/255.f) blue:(45/255.f) alpha:1.0f];

Upvotes: 1

manujmv
manujmv

Reputation: 6445

You need to check the OS version. If it is IOS7, then you can use barTintColor. In IOS6, you can use tintColor

if ([self checkOSVersion] >= 7) {
        [[UINavigationBar appearance] setBarTintColor:[UIColor UIColorFromRGB(0x212121)]];
        [[UINavigationBar appearance] setTintColor:[UIColor whiteColor]];
    } else {
        [[UINavigationBar appearance] setTintColor:[UIColor UIColorFromRGB(0x212121)]];
    }

define the OS version checking method as

- (int)checkOSVersion {

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

Upvotes: 2

Ilario
Ilario

Reputation: 6079

the best solution is detect which version of os is:

-(void)viewWillAppear:(BOOL)animated {

   NSString *ver = [[UIDevice currentDevice] systemVersion];
   int ver_int = [ver intValue];

   if (ver_int < 7) {
       [self.navigationController.navigationBar setTintColor:[UIColor UIColorFromRGB(0x212121)]];
    }

   else {
    self.navigationController.navigationBar.barTintColor = [UIColor UIColorFromRGB(0x212121)];
   }

 }

Upvotes: 5

Neeku
Neeku

Reputation: 3653

[self.navigationController.navigationBar setTintColor:[UIColor redColor]];

Upvotes: 1

thorb65
thorb65

Reputation: 2716

[[UINavigationBar appearance] setTintColor:[UIColor blackColor]];

Upvotes: 7

Related Questions