UITabBarItem图像颜色为灰色

UITabBarItem图像颜色为灰色

本文介绍了UITabBarItem图像颜色为灰色,而原始图像为白色的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I use the following code to create image for my UITabBarItem

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

此tab_img.png由黑色,白色和清晰的颜色组成。但在应用程序中,黑白图像的所有部分都变为灰色。如何将此灰色更改为白色?

This tab_img.png consists of black, white and clear color. But in app all part of image that is black and white turns to grey. How I can change this grey to white ?

推荐答案

在iOS7中,如果您使用IB,您可以继承UITabBarController,然后添加:

In iOS7, if you use IB you can subclass UITabBarController then add this:

+ (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并添加初始化的代码。

if you create the items manualy you must set the UIImageRenderingModeAlwaysOriginal on each icon and add the code from initialize.

这篇关于UITabBarItem图像颜色为灰色,而原始图像为白色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-31 04:30