iOS UINavigationBar tint color appears darker than color set

喜欢而已 提交于 2020-01-01 09:17:58

问题


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

回答1:


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.



来源:https://stackoverflow.com/questions/23090256/ios-uinavigationbar-tint-color-appears-darker-than-color-set

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!