我正在为特定的导航控制器设置自定义外观:
当导航控制器显示时,预期的颜色是RGB(247, 247, 247) - 我已经确认当tableView.background color设置为此值时也是该值 - 但在屏幕上显示的颜色却是RGB(227, 227, 227)。可能有不同的UINavigationBar的外观代理属性改变了在屏幕上显示的颜色吗?
谢谢!
另外,如果我直接使用所需颜色设置barTintColor,则在屏幕上显示的barTintColor仍然比预期的暗。
//Set Cutom Nav Bar Appearance
[[UINavigationBar appearanceWhenContainedIn:[MyNavigationControllerClass class], nil] setBackgroundImage: nil forBarMetrics: UIBarMetricsDefault];
[[UINavigationBar appearanceWhenContainedIn:[MyNavigationControllerClass class], nil] setBarTintColor: self.tableView.backgroundColor];
当导航控制器显示时,预期的颜色是RGB(247, 247, 247) - 我已经确认当tableView.background color设置为此值时也是该值 - 但在屏幕上显示的颜色却是RGB(227, 227, 227)。可能有不同的UINavigationBar的外观代理属性改变了在屏幕上显示的颜色吗?
谢谢!
另外,如果我直接使用所需颜色设置barTintColor,则在屏幕上显示的barTintColor仍然比预期的暗。
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];
解决方案
这是从@Matt的答案中推导出来的解决方案代码。希望能对某些人有所帮助。
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];