xcode UIBarButtonItem 图像保持蓝色而不是图像的原始颜色?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/26854667/
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-15 06:08:51  来源:igfitidea点击:

UIBarButtonItem image stays blue and not the original color of the image?

objective-cxcodeuibarbuttonitem

提问by Jan

How come the icon info.png stays blue and don't comes with the original color of that image? I am using the following code below:

为什么图标 info.png 保持蓝色并且不带有该图像的原始颜色?我正在使用以下代码:

self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"info.png"]
                                                                             style:UIBarButtonItemStylePlain
                                                                            target:self
                                                                            action:@selector(info:)];

回答by Peter

By default, image in UINavigationBar's bar button items is rendered using template mode. You can set it to original.

默认情况下,UINavigationBar 的栏按钮项中的图像使用模板模式呈现。您可以将其设置为原始。

self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithImage:[[UIImage imageNamed:@"info.png"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal]
                                                                         style:UIBarButtonItemStylePlain
                                                                        target:self
                                                                        action:@selector(info:)];

回答by selmad

Swift 3:

斯威夫特 3:

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

回答by nivritgupta

I know this is too late to answer this question but I see there is a very simple way to solve this issue instead of doing some changes in the code

我知道现在回答这个问题为时已晚,但我发现有一种非常简单的方法可以解决这个问题,而不是对代码进行一些更改

using Xcode Go to the Assets --Select Image --- check Render as and select Original image instead of default property .

使用 Xcode 转到 Assets --Select Image --- 检查 Render as 并选择 Original image 而不是默认属性。

enter image description here

在此处输入图片说明

回答by Amrit Sidhu

You can it from assets as well. Go to Assets.xcassets >> Select the image that is being used as barbutton item image. Tap on attribute inspector in right side panel. Choose render as to orignial image. It will be default earlier. You will now see colored image.

您也可以从资产中获取。转到 Assets.xcassets >> 选择用作栏按钮项目图像的图像。点击右侧面板中的属性检查器。选择渲染为原始图像。之前会默认。您现在将看到彩色图像。

回答by choofie

Swift 4:

斯威夫特 4:

let image = UIImage(named: "imageName")?.withRenderingMode(.alwaysOriginal)
navigationItem.leftBarButtonItem = UIBarButtonItem(image: image, style: .plain, target: self, action: #selector(leftBarButtonPressed))

回答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)

or simply

或者干脆

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

回答by carlodurso

Because the color of barButtonItems in your app is related to the tintColor property on the application's window.

因为应用程序中 barButtonItems 的颜色与应用程序窗口上的 tintColor 属性有关。

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    self.window.tintColor = [UIColor redColor];
    return YES;
}

回答by Jan

Ok, got it... I set the image to it's original state first.

好的,明白了...我首先将图像设置为原始状态。

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

self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithImage:image
                                                                             style:UIBarButtonItemStylePlain
                                                                            target:self
                                                                            action:@selector(info:)];

回答by Sofeda

Change the bar button item tint color from the storyboard. Or color from the image in storyboard.

从故事板更改条形按钮项的色调颜色。或者从故事板中的图像颜色。

The color should be your expected color as hex or rgb.

颜色应该是您期望的颜色,如十六进制或 rgb。