13

我使用以下代码为我创建图像UITabBarItem

self.tabBarItem.image = [UIImage imageNamed:@"tab_img.png"];

此 tab_img.png 由黑色、白色和透明色组成。但是在应用程序中,黑白图像的所有部分都变成了灰色。我怎样才能把这个灰色变成白色?

这是我使用的图像

在此处输入图像描述

4

7 回答 7

33

在 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 并从初始化添加代码。

于 2014-04-08T12:34:23.317 回答
16

如果您使用图像资产,只需将图像(选定和未选定图像)的“渲染为”字段设置为“原始图像”(示例

然后在您的选项卡栏项目上的 xib 设置“图像”和“选定图像”字段(示例

于 2017-09-01T09:30:48.557 回答
10

设置选中和未选中的图像。

[self.tabBarItem setFinishedSelectedImage:[UIImage imageNamed:@"mehr_icon"] withFinishedUnselectedImage:[UIImage imageNamed:@"mehr_icon"]];
于 2013-02-26T10:54:41.823 回答
3

UITabBarItems 的图像只能是 alpha 通道!但是,不透明部分将仅显示为灰色(如果选中则为蓝色)!

看看:http ://devinsheaven.com/creating-uitabbar-uitoolbar-icons-in-adobe-illustrator/

于 2013-02-26T10:14:46.877 回答
0

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.

于 2013-11-22T07:33:51.330 回答
0

我曾经遇到过同样的问题,我只使用带有白色和 alpha 的图像,就像这张图片一样

我用self.tabBarItem.image = [UIImage imageNamed:@"Liste"];

于 2013-02-26T10:19:43.963 回答
0

对我来说最好的方法是改变图像颜色。

 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)
    }
于 2018-08-13T19:18:11.337 回答