Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

iOS UINavigationBar tint color appears darker than color set

I am setting a custom appearance for a specific navigation controller:

//Set Cutom Nav Bar Appearance
[[UINavigationBar appearanceWhenContainedIn:[MyNavigationControllerClass class], nil] setBackgroundImage: nil forBarMetrics: UIBarMetricsDefault];
[[UINavigationBar appearanceWhenContainedIn:[MyNavigationControllerClass class], nil] setBarTintColor: self.tableView.backgroundColor];

When the navigation controller is displayed, the expected color is RGB(247, 247, 247) - which I have double checked is also the value of the tableView.background color when the value is set - but it appears on screen as RGB( 227, 227, 227). Could there be a different property of UINavigationBar's appearance proxy that is changing the color displayed on screen?

Thanks!

Edit:

Additionally, if I set the barTintColor directly using the desired color, the barTintColor displayed on screen is still darker than expected:

UIColor* navBarBackgroundColor = [UIColor colorWithRed: (247.0 / 255.0) green: (247.0 / 255.0) blue: (247.0 / 255.0) alpha: 1];
//Set Cutom Nav Bar Appearance
[[UINavigationBar appearanceWhenContainedIn:[MyNavigationControllerClass class], nil] setBackgroundImage: nil forBarMetrics: UIBarMetricsDefault];
[[UINavigationBar appearanceWhenContainedIn:[MyNavigationControllerClass class], nil] setBarTintColor: navBarBackgroundColor];

Solution

Here is the solution code derived from @Matt's answer. Hope it helps someone out

CGRect rect = CGRectMake(0.0f, 0.0f, 1.0f, 1.0f);
UIGraphicsBeginImageContext(rect.size);
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextSetFillColorWithColor(context, [self.tableView.backgroundColor CGColor]);
CGContextFillRect(context, rect);
UIImage *navBarImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();

//Set Cutom Nav Bar Appearance
[[UINavigationBar appearanceWhenContainedIn:[MyNavigationControllerClass class], nil] setBackgroundImage: navBarImage forBarMetrics: UIBarMetricsDefault];
like image 907
Glavid Avatar asked Apr 15 '14 17:04

Glavid


1 Answers

The way to set the color of a navigation bar exactly is the precise opposite of what you're doing. You are giving it a tint color and no background image. Instead, give it a background image consisting of a rectangle of the desired color - and no tint color.

Also set its translucent to NO.

like image 135
matt Avatar answered Nov 22 '22 22:11

matt