ios 通过xcode5的xib设计时导航栏按钮项目图像颜色不同

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

Navigation bar button item image color is different when design through xib of xcode5

iosiphoneobjective-cxcodeios5

提问by Bug

I am creating navigation bar button using xib but when i going to set image to bar button then image colour is different as original image.

我正在使用 xib 创建导航栏按钮,但是当我将图像设置为栏按钮时,图像颜色与原始图像不同。

Here is my orignal image.

这是我的原始图像。

Here is my orignal image

这是我的原始图片

And after adding that image on navigation bar button item than it look like this

在导航栏按钮项上添加该图像后,它看起来像这样

Aafter adding that image on navigation bar

在导航栏上添加该图像后

回答by Daniel McCarthy

First, I agree with @Desdenova's comment.
The two images do not look the same, one has hard right angle edges for each line, and the other rounded.
Make sure you are using the correct image file.
If this is the case, awesome, problem solved without deviating from your xibimplementation. If not, just do it programmatically (as per @shankars code).
But another thing to note, I've run into problems setting custom image files to buttons, where the image gets tweaked... make sure to use UIImageRenderingModeAlwaysOriginalwhen setting the image to the button:

首先,我同意@Desdenova 的评论。
两个图像看起来不一样,一个图像的每条线都有硬直角边缘,另一个是圆形的。
确保您使用的是正确的图像文件。
如果是这种情况,真棒,问题解决了,而不会偏离您的xib实现。如果没有,只需以编程方式进行(根据@shankars 代码)。
但另一件要注意的事情是,我在将自定义图像文件设置为按钮时遇到了问题,其中图像会被调整......确保UIImageRenderingModeAlwaysOriginal在将图像设置为按钮时使用:

Objective-C:

目标-C:

[button setImage:[[UIImage imageNamed:@"imageName.png"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal] forState:UIControlStateNormal];

Swift:

迅速:

someBarButtonItem.image = UIImage(named: "yourPictureName")?.imageWithRenderingMode(UIImageRenderingMode.AlwaysOriginal)

Swift 3:

斯威夫特 3:

someBarButtonItem.image = UIImage(named:"myImage")?.withRenderingMode(.alwaysOriginal)

回答by Iftikhar Ali Ansari

This is sample working code

这是示例工作代码

UIImage *myImage = [UIImage imageNamed:@"myImageFile.png"];
myImage = [myImage imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
UIBarButtonItem *menuButton = [[UIBarButtonItem alloc] initWithImage:myImage style:UIBarButtonItemStylePlain target:self action:@selector(menuObject:)];
self.navigationItem.leftBarButtonItem = menuButton;

回答by Max

Hopefully I am not too late to add an answer of my own, but within Assets.xcassets, you can click on your image and in the attributes inspector, under Rendar Asset it to Original Image

希望我不会太晚添加我自己的答案,但是在 Assets.xcassets 中,您可以单击您的图像并在属性检查器中,在Rendar As 下将其设置为Original Image

enter image description here

在此处输入图片说明

回答by codercat

Because ios7 storyboard have issue i faced to fix like below. set your tint color as image color it works

因为 ios7 故事板有我面临的问题,如下所示。将您的色调颜色设置为它有效的图像颜色

enter image description here

在此处输入图片说明

回答by shankar

You can create navigation bar button programmatically instead of direct storyboard, this will not affect original image color

您可以以编程方式创建导航栏按钮而不是直接故事板,这不会影响原始图像颜色

self.navigationItem.leftBarButtonItem=[self backButton];

- (UIBarButtonItem *)backButton
{
   UIImage *image = [UIImage imageNamed:@"image.png"];
   CGRect buttonFrame = CGRectMake(0, 0, image.size.width, image.size.height);

   UIButton *button = [[UIButton alloc] initWithFrame:buttonFrame];
   //[button addTarget:self action:@selector(backButtonPressed) forControlEvents:UIControlEventTouchUpInside];
   [button setImage:image forState:UIControlStateNormal];

   UIBarButtonItem *item= [[UIBarButtonItem alloc] initWithCustomView:button];

   return item;
}

回答by bhavya kothari

You need to set tint color as well - which worked for me - You can generate UIBarButtonItem via code as follows:

您还需要设置色调颜色 - 这对我有用 - 您可以通过代码生成 UIBarButtonItem 如下:

#define setTurqoiseColor [UIColor colorWithRed:68.0f/255.0f green:181.0f/255.0f blue:223.0f/255.0f alpha:1.0]

UIBarButtonItem *menuButton = [[UIBarButtonItem alloc] initWithImage:buttonImage style:UIBarButtonItemStyleBordered target:self action:@selector(toggleMenu)];
menuButton.tintColor = setTurqoiseColor;