【发布时间】:2015-12-20 16:08:54
【问题描述】:
【问题讨论】:
-
尝试设置
tintColor有时它可以帮助我设置图像的实际颜色。
标签: ios swift storyboard
【问题讨论】:
tintColor 有时它可以帮助我设置图像的实际颜色。
标签: ios swift storyboard
使用 UIBarButton 的tintColor 为图像设置所需的颜色。
如果绝对需要使用原始图像颜色,请使用它来设置图像:
[aBarButton setImage:[[UIImage imageNamed:@"xyz.png"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal]];
【讨论】:
The docs对此有点模棱两可
栏上显示的图像来自该图像。如果这 图像太大而无法放在栏上,它被缩放以适合。通常, 工具栏和导航栏图像的大小为 20 x 20 点。这 源图像中的 alpha 值用于创建图像——不透明 值被忽略。
基本上这是说您提供的图像不会是实际显示的图像。相反,系统使用图像的 alpha 蒙版和项目的 tintColor 来生成最终显示。
【讨论】:
tintColor 结合使用,尝试将其设置为绿色。如果您想要不止一种纯色,则需要使用其他方法(请参阅Ishan's answer)
以编程方式添加图像
[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)
【讨论】: