iOS 7 UIButtonBarItem图像不着色

iOS 7 UIButtonBarItem图像不着色,ios,iphone,objective-c,ios7,uibuttonbaritem,Ios,Iphone,Objective C,Ios7,Uibuttonbaritem,在我的导航栏上,我有两个带有自定义图标的右按钮(图标图像是白色的,与iOS 6的基本配色方案配合得很好) 在iOS 7下,使用initWithTitle加载图像(请参见代码片段1)将图标中的“白色”替换为适当的全局色调(在本例中为特定的深蓝色) 代码片段1: UIBarButtonItem *refreshButton = [[UIBarButtonItem alloc] initWithTitle:@"" style:(UIBarButtonItemStyle) UIBarButtonSyst

在我的导航栏上,我有两个带有自定义图标的右按钮(图标图像是白色的,与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"];
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];
然而,我需要使用initWithCustomView来克服导致图标移出视图的行为上的奇怪变化。其基本思想是专门设置图标的大小。initWithCustomView解决了大小调整问题,但不显示带有全局色调的按钮图像,它们以图像的颜色(白色)显示。代码片段2显示了我如何使用initWithCustomView创建按钮

代码片段2

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

refreshButton.image = [UIImage imageNamed:@"RefreshIcon.png"];
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]];//不起作用

它们不会覆盖图像的白色。这几乎就像是在视图查看全局着色颜色后创建自定义视图一样

如何确保按钮图标呈现全局色调


谢谢

您必须解决第一个代码段中出现的问题,或者必须创建一个UIButton子类,该子类使用其图像作为遮罩,以在
drawRect:
中显示着色颜色


我推荐第一种方法。

只是想把它放到一个根注释中,为“答案”复选标记提供更好的上下文,并提供更好的格式


我能想出这个!您可以告诉图像始终作为模板渲染,这将强制其采用全局着色颜色

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

如果不设置,默认值为“UIImageRenderingModeAutomatic”,这意味着它将根据上下文作为模板或原始图像进行渲染。

谢谢,但第二个代码段实际上是第一个问题的解决方法,至少我认为我需要这样做。这两个按钮最初渲染得很好,但当我切换到另一个视图然后返回时,它们似乎会换行到下一行。这是一个更难描述的问题,我认为仅仅为图标设置一个固定的宽度会更容易,而且它确实解决了图标移动的问题,但却让我陷入了色调的两难境地。我能够解决这个问题!您可以告诉图像始终作为模板渲染,这将强制其采用全局着色颜色。UIImage*iconRefreshButton=[UIImage ImageName:@“MyIconFilename.png”];iconRefreshButton=[iconRefreshButton imageWithRenderingMode:UIImageRenderingModeAlwaysTemplate];默认值(如果未设置)为“UIImageRenderingModeAutomatic”,这意味着它将根据上下文作为模板或原始图像进行渲染。