iOS导航栏的色调颜色比所设置的颜色更暗

8
我正在为特定的导航控制器设置自定义外观:
//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];

请问您能否添加一行代码,指定RGB值的位置? - William Riley
可能是条形图的 alpha 或透明度设置吗?你有检查过吗? - Michal
@William tableView的背景颜色被转发到navigationBar中是在storyboard中设置的。即使我明确设置所需的颜色,它仍然显示为比设置的颜色更暗(请参见上面的代码)。 - Glavid
@Glavid,下面的回答没有帮助吗?因为他们指出导航栏的半透明可能是原因。表视图的背景颜色再加上半透明导航栏的颜色可能导致你看到的较暗的颜色。 - William Riley
@William 问题已更新! - Glavid
1个回答

14

精确定义导航栏颜色的方法恰恰相反于您所执行的操作。您正在为其添加一个着色颜色但没有背景图片。相反,请将其背景图片设置为所需颜色的矩形 - 并且不要添加着色颜色。

同时将其 translucent 设置为 NO。


似乎以这种方式设置背景颜色很奇怪,但它确实有效。我猜barTintColors在显示屏上显示之前会被操作系统修改,所以就像你说的这是"获取准确颜色的最佳方式"。感谢帮助..将解决方案代码发布回问题中。 - Glavid
1
这部分涉及到整个半透明的业务,以及“色调”一词的含义(需要认真对待)。 - matt

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接