简体   繁体   English

iOS栏项目图像显示错误的颜色

[英]iOS Bar Item image displaying wrong color

I have a bar with Bar Items, my .png image has green color, but when i add it to storyboard it's displaying as blue. 我有一个带条形图的条形图,我的.png图像有绿色,但当我将它添加到故事板时,它显示为蓝色。

How can i make it display the image as it is? 如何让它显示图像?

在此输入图像描述

在此输入图像描述

Use tintColor of UIBarButton to set the desired color for the image. 使用UIBarButton的tintColor设置图像的所需颜色。 If its absolutely necessary to use original image colors, use this to set the image: 如果绝对需要使用原始图像颜色,请使用此设置图像:

[aBarButton setImage:[[UIImage imageNamed:@"xyz.png"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal]];

The docs are a little ambiguous about this 文档对此有点含糊不清

The images displayed on the bar are derived from this image. 条形图上显示的图像来自此图像。 If this image is too large to fit on the bar, it is scaled to fit. 如果此图像太大而无法放在条形图上,则会缩放以适合该图像。 Typically, the size of a toolbar and navigation bar image is 20 x 20 points. 通常,工具栏和导航栏图像的大小为20 x 20磅。 The alpha values in the source image are used to create the images—opaque values are ignored. 源图像中的alpha值用于创建图像 - 忽略不透明值。

Essentially what this is saying is the image you supply will not be what is actually displayed. 基本上这就是说你提供的图像不是实际显示的图像。 Instead the system uses the alpha mask of the image and the tintColor of the item to generate the final display. 相反,系统使用图像的alpha蒙版和项目的tintColor来生成最终显示。

add image programmatically 以编程方式添加图像

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

if its not work then try this:- 如果它不起作用,那么试试这个: -

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;

if its not work then try this:- 如果它不起作用,那么试试这个: -

#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;

To set tint color of bar item global, in your App Delegate, add these lines of code 要设置条形项全局的色调颜色,请在App Delegate中添加以下代码行

UIBarButtonItem *barButtonAppearance = [UIBarButtonItem appearance];
[barButtonAppearance setTintColor:[UIColor redColor]]; // set to your color
[[UIBarButtonItem appearance] setTintColor:[UIColor redColor]];

You have to set your UITabBar tintColor. 你必须设置你的UITabBar tintColor。

在此输入图像描述

If you would like to add a custom color / gradient you can set your tabBarItem image and selectedImage property as follow: 如果要添加自定义颜色/渐变,可以设置tabBarItem图像和selectedImage属性,如下所示:

 customTabBarItem.selectedImage = UIImage(named: "customSelectedImage")!.imageWithRenderingMode(.AlwaysOriginal)
 customTabBarItem.image = UIImage(named: "customUnselectedImage")!.imageWithRenderingMode(.AlwaysOriginal)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM