我使用以下代码为我创建图像UITabBarItem
self.tabBarItem.image = [UIImage imageNamed:@"tab_img.png"];
此 tab_img.png 由黑色、白色和透明色组成。但是在应用程序中,黑白图像的所有部分都变成了灰色。我怎样才能把这个灰色变成白色?
我使用以下代码为我创建图像UITabBarItem
self.tabBarItem.image = [UIImage imageNamed:@"tab_img.png"];
此 tab_img.png 由黑色、白色和透明色组成。但是在应用程序中,黑白图像的所有部分都变成了灰色。我怎样才能把这个灰色变成白色?
在 iOS7 中,如果你使用 IB,你可以继承 UITabBarController 然后添加:
+ (void)initialize
{
//the color for the text for unselected tabs
[UITabBarItem.appearance setTitleTextAttributes:@{NSForegroundColorAttributeName : [UIColor redColor]} forState:UIControlStateNormal];
//the color for selected icon
[[UITabBar appearance] setSelectedImageTintColor:[UIColor whiteColor]];
}
- (void)viewDidLoad
{
[super viewDidLoad];
if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 7) {
for (UITabBarItem *tbi in self.tabBar.items) {
tbi.image = [tbi.image imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
}
}
}
如果您手动创建项目,则必须在每个图标上设置 UIImageRenderingModeAlwaysOriginal 并从初始化添加代码。
设置选中和未选中的图像。
[self.tabBarItem setFinishedSelectedImage:[UIImage imageNamed:@"mehr_icon"] withFinishedUnselectedImage:[UIImage imageNamed:@"mehr_icon"]];
UITabBarItems 的图像只能是 alpha 通道!但是,不透明部分将仅显示为灰色(如果选中则为蓝色)!
看看:http ://devinsheaven.com/creating-uitabbar-uitoolbar-icons-in-adobe-illustrator/
Only Way is to go to IB(interface builder) and select the UITabBarItem in your View Controller and go to "file inspector" scroll down and you will see Global Tint her you can set it to no color or any color that you want it will take effect for the selected image.
as per the following code is concern
setFinishedSelectedImage: withFinishedUnselectedImage:;
this is no longer available in iOS 7 rather we can use
[yourCustomTabBarItem setSelectedImage:---];
but this will also take effect of this Global tint color.
我曾经遇到过同样的问题,我只使用带有白色和 alpha 的图像,就像这张图片一样
我用self.tabBarItem.image = [UIImage imageNamed:@"Liste"];
对我来说最好的方法是改变图像颜色。
func imageWithColor(_ color: UIColor) -> UIImage? {
UIGraphicsBeginImageContextWithOptions(size, false, scale)
let context = UIGraphicsGetCurrentContext()!
context.translateBy(x: 0, y: size.height)
context.scaleBy(x: 1.0, y: -1.0)
context.setBlendMode(.normal)
let rect = CGRect(x: 0, y: 0, width: size.width, height: size.height)
context.clip(to: rect, mask: cgImage!)
color.setFill()
context.fill(rect)
let newImage = UIGraphicsGetImageFromCurrentImageContext()!
UIGraphicsEndImageContext()
newImage.accessibilityIdentifier = accessibilityIdentifier
return newImage
}
然后,您可以更新 tabBarItem 的属性,例如 image 和 selectedImage:
func setupColorAttributes(to item: UITabBarItem) {
item.image = item.image?.imageWithColor(.white)?.withRenderingMode(.alwaysOriginal)
item.selectedImage = item.image?.imageWithColor(.highLight)?.withRenderingMode(.alwaysOriginal)
}