iphone – UITabBarItem图像颜色为灰色,原始图像为白色

我使用以下代码为我的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,并从初始化中添加代码.

相关文章

当我们远离最新的 iOS 16 更新版本时,我们听到了困扰 Apple...
欧版/美版 特别说一下,美版选错了 可能会永久丧失4G,不过只...
一般在接外包的时候, 通常第三方需要安装你的app进行测...
前言为了让更多的人永远记住12月13日,各大厂都在这一天将应...