繁体   English   中英

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

[英]iOS Bar Item image displaying wrong color

我有一个带条形图的条形图,我的.png图像有绿色,但当我将它添加到故事板时,它显示为蓝色。

如何让它显示图像?

在此输入图像描述

在此输入图像描述

使用UIBarButton的tintColor设置图像的所需颜色。 如果绝对需要使用原始图像颜色,请使用此设置图像:

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

文档对此有点含糊不清

条形图上显示的图像来自此图像。 如果此图像太大而无法放在条形图上,则会缩放以适合该图像。 通常,工具栏和导航栏图像的大小为20 x 20磅。 源图像中的alpha值用于创建图像 - 忽略不透明值。

基本上这就是说你提供的图像不是实际显示的图像。 相反,系统使用图像的alpha蒙版和项目的tintColor来生成最终显示。

以编程方式添加图像

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

如果它不起作用,那么试试这个: -

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;

如果它不起作用,那么试试这个: -

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

要设置条形项全局的色调颜色,请在App Delegate中添加以下代码行

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

你必须设置你的UITabBar tintColor。

在此输入图像描述

如果要添加自定义颜色/渐变,可以设置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