1

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?

user7388
  • 1,741
  • 2
  • 19
  • 25
orthehelper
  • 4,009
  • 10
  • 40
  • 67
  • possible duplicate of [How to change navigation bar color in iOS 7 or 6?](http://stackoverflow.com/questions/18177010/how-to-change-navigation-bar-color-in-ios-7-or-6) – Tarek Hallak Nov 28 '13 at 09:42

6 Answers6

7
[[UINavigationBar appearance] setTintColor:[UIColor blackColor]];
thorb65
  • 2,696
  • 2
  • 27
  • 37
  • 1
    Its good , but using appearance it will change the colour of navigation bar universally not for a particular view's navigation bar. – Anurag Kabra Nov 28 '13 at 09:38
5

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

 }
Ilario
  • 5,979
  • 2
  • 32
  • 46
5

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];
}
Lisarien
  • 1,136
  • 1
  • 12
  • 24
2

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;
}
manujmv
  • 6,450
  • 1
  • 22
  • 35
1
[self.navigationController.navigationBar setTintColor:[UIColor redColor]];
Neeku
  • 3,646
  • 8
  • 33
  • 43
1
self.navigationController.navigationBar.tintColor=[UIColor colorWithRed:(45/255.f) green:(45/255.f) blue:(45/255.f) alpha:1.0f];
Muralikrishna
  • 1,044
  • 10
  • 17