带UIImage的UIBarButtonItem始终着色iOS 7 [英] UIBarButtonItem with UIImage Always Tinted iOS 7

查看:81
本文介绍了带UIImage的UIBarButtonItem始终着色iOS 7的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将包含 UIImage UIBarButtonItem 添加到 UIToolbar 。图像不断被着色,我无法将其显示为原始彩色图像 - 我想要做的就是在 UIBarButtonItem 中逐字显示图像!我按照iOS 7过渡指南中的说明将图像渲染模式设置为 UIImageRenderingModeAlwaysOriginal

I'm trying to add a UIBarButtonItem containing a UIImage to a UIToolbar. The image keeps being tinted and I can't get it to show as the original colored image - all I want to do is display an image, verbatim, in a UIBarButtonItem! I'm following the directions in the iOS 7 transition guide to set the image rendering mode to UIImageRenderingModeAlwaysOriginal.

UIImage *image = [UIImage imageNamed:@"myImage.png"];
image = [image imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];

UIBarButtonItem *ratingImage = [[UIBarButtonItem alloc] initWithImage:image style:UIBarButtonItemStyleBordered target:nil action:nil];

[toolbar setItems:[NSArray arrayWithObjects:ratingImage, nil] animated:YES];

有一点需要注意的是我在加载时正确设置了应用程序主UIWindow的tintColor ...也许这对我的问题并不重要,但我想我会提到它。

One thing to note is that I set the tintColor for the main UIWindow of my app right when it loads...maybe this isn't important with regard to my issue, but thought I'd mention it.

推荐答案

我花了一个晚上试图解决这个问题。你非常接近解决方案。
诀窍是用渲染模式实例化UIImage。

I spent an evening trying to figure this out as well. You were very close to the solution. The trick is to instantiate the UIImage with the rendering mode.

而不是:

 UIImage *image = [UIImage imageNamed:@"myImage.png"];
 image = [image imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];

这样做:

 UIImage *image = [[UIImage imageNamed:@"myImage.png"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];

它有效!

在我的我已经将导航栏拖到IB中的viewcontroller,并添加了BarButtonItem。但是不要在IB中提供项目图像。制作一个插座,并通过这样做为它分配UIImage(就像我们在上面创建的那样):

In my case, I had dragged a Navigation bar to my viewcontroller in the IB, and added the BarButtonItem. But don't provide the item an image in the IB. Make an outlet and assign it the UIImage (like we created above) by doing this:

[myCustomBarButtonItem setImage:image];

希望这适合你。

这篇关于带UIImage的UIBarButtonItem始终着色iOS 7的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆