在我的导航栏上,我有几个具有自定义图标的右barbuttonitems(图像是白色的,与iOS 6的基本配色方案很好地搭配使用)。

在iOS 7下,使用initwithtitle加载图像(请参阅代码段1)用适当的全局色调代替图标中的“白色”颜色(在这种情况下,深蓝色的特定颜色)

代码片段1:

UIBarButtonItem *refreshButton = [[UIBarButtonItem alloc] initWithTitle:@"" style:(UIBarButtonItemStyle) UIBarButtonSystemItemCancel target:(self) action:@selector(refreshList)];

refreshButton.image = [UIImage imageNamed:@"RefreshIcon.png"];

但是,我需要使用initwithCustomView来克服导致图标移出视图的行为的奇怪变化。基本思想是专门设置图标的大小。 InitwithCustomView解决了尺寸问题,但不会显示带有全局色调的按钮图像,它们以图像的颜色(白色)显示。代码段2显示了我如何使用initwithCustomView创建按钮。

代码段2:

CGRect frameCustomButton2 = CGRectMake(0.0, 0.0, 18.0, 18.0);
UIButton *customButton2 = [[UIButton alloc] initWithFrame:frameCustomButton2];
[customButton2 setBackgroundImage:iconRefreshButton forState:UIControlStateNormal];
UIBarButtonItem *barCustomButton2 =[[UIBarButtonItem alloc] initWithCustomView:customButton2 ];
barCustomButton2.image = iconRefreshButton;
[customButton2 addTarget:self action:@selector(refreshList) forControlEvents:UIControlEventTouchUpInside];

当然,所有这些代码都在(void)ViewDidload中。我尝试了类似的事情:

barCustomButton2.tintColor = [UIColor blackColor];  //doesn't work

或[BarbuttonAppearance settintColor:[Uicolor BlackColor]]; //不起作用

而且它们不会覆盖图像的白色。自定义视图的创建几乎是在查看全局色彩颜色之后发生的?

如何确保按钮图标为全局色调?

谢谢!

有帮助吗?

解决方案

只是想将其纳入根评论,以更好地为“答案”选中标记提供更好的背景,并提供更好的格式。

我能够弄清楚这个!您可以告诉图像始终呈现为模板,这将迫使其采用全局色彩。

UIImage *iconRefreshButton = [UIImage imageNamed:@"MyIconFilename.png"];
iconRefreshButton = [iconRefreshButton imageWithRenderingMode:UIImageRenderingModeAlwaysTemplate];

默认值(如果不设置)为“ UIIMAGERENDERINGMODEAUTOMATION”,这意味着它将根据上下文呈现为模板或原始图像。

其他提示

您要么必须解决第一个代码段的问题,要么必须创建一个使用其图像作为掩码的Uibutton子类,以显示色调颜色 drawRect:.

我建议第一种方法。

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top