微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

iOS 7 UIButtonBarItem图像没有色调

在我的导航栏上,我有几个带有自定义图标的rightBarButtonItems(图标图像为白色,适用于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];

认情况下,如果不设置,则为“UIImageRenderingModeAutomatic”,这意味着它将基于上下文呈现为模板或原始图像.

原文地址:https://www.jb51.cc/iOS/332383.html

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐