objective-c 带有 UIImage 的 UIBarButtonItem 始终着色 iOS 7

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19372269/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-03 21:06:31  来源:igfitidea点击:

UIBarButtonItem with UIImage Always Tinted iOS 7

objective-cios7uibarbuttonitemuitoolbar

提问by DiscDev

I'm trying to add a UIBarButtonItemcontaining a UIImageto 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.

我正在尝试添加一个UIBarButtonItem包含 aUIImage到 a UIToolbar。图像不断被着色,我无法将其显示为原始彩色图像 - 我想要做的就是逐字显示图像UIBarButtonItem!我正在按照 iOS 7 转换指南中的说明将图像渲染模式设置为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];

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.

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

回答by ScorpionKing2k5

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。

Instead of doing:

而不是做:

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

do this:

做这个:

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

and it works!

它有效!

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:

就我而言,我已将导航栏拖到 IB 中的视图控制器,并添加了 BarButtonItem。但不要在 IB 中为该项目提供图像。通过执行以下操作创建一个插座并为其分配 UIImage(就像我们在上面创建的那样):

[myCustomBarButtonItem setImage:image];

Hope this works for you.

希望这对你有用。

回答by andlin

UIImageRenderingModeAlwaysOriginal can also be set by selecting the image in your Assets.xcassets "folder" in XCode and setting the "Render as" dropdown to "Original image".

UIImageRenderingModeAlwaysOriginal 也可以通过在 XCode 的 Assets.xcassets“文件夹”中选择图像并将“渲染为”下拉列表设置为“原始图像”来设置。

回答by jesses.co.tt

For Swift 2.1+ it would look like this:

对于 Swift 2.1+,它看起来像这样:

let image : UIImage? = UIImage(named:"myImage.png")!.imageWithRenderingMode(UIImageRenderingMode.AlwaysOriginal)

UPDATED Swift 3

更新的斯威夫特 3

let image : UIImage? = UIImage(named:"myImage.png")!.withRenderingMode(.alwaysOriginal)

回答by John Scalo

The accepted answer is fine but if you placed the UIBarButtonItemin a storyboard or xib then you can just:

接受的答案很好,但如果你把它放在UIBarButtonItem故事板或 xib 中,那么你可以:

  • Go to the Assets catalog where the image lives
  • Select the image
  • Go to the attributes inspector (cmd-opt-4)
  • Set "Render As" to "Original Image"
  • 转到图像所在的资产目录
  • 选择图片
  • 转到属性检查器 (cmd-opt-4)
  • 将“渲染为”设置为“原始图像”

Only do this if you want allinstances of this image to show up without tinting.

仅当您希望此图像的所有实例都显示而不着色时才执行此操作。

enter image description here

在此处输入图片说明

回答by Tim Crowley

If you want it to work for versions of iOS less than v7, you might need to to this:

如果您希望它适用于低于 v7 的 iOS 版本,您可能需要这样做:

UIImage *image = [UIImage imageNamed:@"myImage.png"];
@try {
  image = [image imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
} @catch (NSException *exception) {
}

Since imageWithRenderingMode: is an iOS 7 method, you'll get an exception if you try and use it with a lesser version.

由于 imageWithRenderingMode: 是一种 iOS 7 方法,如果您尝试将其与较低版本一起使用,则会出现异常。