UITabBarItem图像颜色为灰色,而原始图像为白色

ij_*_*ij_ 11 iphone objective-c ipad ios

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

self.tabBarItem.image = [UIImage imageNamed:@"tab_img.png"];
Run Code Online (Sandbox Code Playgroud)

tab_img.png由黑色,白色和清晰的颜色组成.但在应用程序中,黑白图像的所有部分都变为灰色.我怎么能把这个灰色变成白色?

这是我使用的图像

在此输入图像描述

Cor*_*ian 34

在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];
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

如果您手动创建项目,则必须在每个图标上设置UIImageRenderingModeAlwaysOriginal并添加初始化的代码.

  • 谢谢......只需添加一个:还应设置tbi.selectedImage = [tbi.selectedImage imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal]; (2认同)

fib*_*chi 10

设置选定和未选择的图像.

[self.tabBarItem setFinishedSelectedImage:[UIImage imageNamed:@"mehr_icon"] withFinishedUnselectedImage:[UIImage imageNamed:@"mehr_icon"]];
Run Code Online (Sandbox Code Playgroud)


小智 9

如果您正在使用图像资源,只需将图像的"渲染为"字段(选定和未选择的图像)设置为"原始图像"(示例)

然后在你的xib中设置Tab Bar项目中的"Image"和"Selected Image"字段(示例)

  • 谢谢,你也拯救了我的日子。 (2认同)

blu*_*lub 3

UITabBarItems 的图像应该只是 alpha 通道!不过,不透明部分只会显示灰色(如果选择蓝色)!

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

  • 对于 iOS 7+ 来说并非如此。您可以使用 `- imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal` (3认同)