Search code examples
iosuinavigationbaruiappearance

UIAppearence only working with system colors in iOS?


I am trying to change the color of the navigation bars in my app with UIAppearance.

But only when I use a system color, it works:

    UINavigationBar *navigationBarAppearance = [UINavigationBar appearance];

    [navigationBarAppearance setBarTintColor:[[UIColor alloc] initWithRed:220.0f green:47.0f blue:40.0f alpha:100.0f]]; // does not work

    [navigationBarAppearance setBarTintColor:[UIColor colorWithRed:220.0f green:47.0f blue:40.0f alpha:100.0f]]; // does not work

    [navigationBarAppearance setBarTintColor:[UIColor redColor]]; // works

Any suggestions?


Solution

  • The method

    colorWithRed:green:blue:alpha:
    

    accept four value between 0.0 and 1.0. So if you have the components from 0.0 to 255.0 you need to normalize with a division by 255.0f.

    [UIColor alloc] initWithRed:220.0f/255.0f green:47.0f/255.0f blue:40.0f/255.0f alpha:100.0f/255.0f]