Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

UITabBarItem image color is grey while original image is white

I use the following code to create image for my UITabBarItem

self.tabBarItem.image = [UIImage imageNamed:@"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 ?

this is image that I use

enter image description here

like image 387
ij_ Avatar asked Feb 26 '13 10:02

ij_


People also ask

How do I change the background color of a tab bar in Swift?

backgroundColor = UIColor(red:1, green:0, blue:0, alpha:1) / UITabBar. appearance(). tintColor = UIColor(red: 1, green: 0, blue: 0, alpha: 1) // New!! func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {...}


2 Answers

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];
        }
    }
}

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

like image 122
Cornel Damian Avatar answered Sep 20 '22 00:09

Cornel Damian


If you are using image assets, just set the field "Render As" of your images (selected and unselected images) to "Original Image" (example)

Then in your xib set "Image" and "Selected Image" fields on your Tab Bar Item (example)

like image 26
luca.giorgetti.spot Avatar answered Sep 20 '22 00:09

luca.giorgetti.spot