UITabBarController unselected icon image tint

2020-04-01 10:25发布

问题:

I have a UITabBarController that I am trying to modify. Right now the UNselected tab icon images are default gray. I know that you cant change the tint of these UNselected icon images programmatically. Apple says that the tint is controlled by the actual tint of the png file itself. So if you want a white or green icon image then you have to use a png that displays the image as white or green etc. After that you must use UIImageRenderingModeAlwaysOriginal AND initWithTitle:image:selectedImage: I used this code in my FirstViewController.m and i placed it in the -(void)viewDidLoad. However, I am getting a parse issue: expected identifier and its pointing at the bracket after nil. Does anybody see the issue with this code?

//
//  FirstViewController.m
//  tabmock5
//
//  Created by USER on 9/26/13.
//  Copyright (c) 2013 USER. All rights reserved.
//

#import "FirstViewController.h"

@interface FirstViewController ()

@end

@implementation FirstViewController

- (void)viewDidLoad
{
[[UIImage imageNamed:@"white_stats.png"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];

[self.tabBarItem initWithTitle:[nil]image:[UIImage imageNamed:@"white_stats.png"]selectedImage:[UIImage imageNamed:@"white_stats.png"]];

[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
}

回答1:

[[UIImage imageNamed:@"white_stats.png"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];

This isn't saving the image anywhere. Change it to:

UIImage *myImage = [[UIImage imageNamed:@"white_stats.png"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];

And this:

[self.tabBarItem initWithTitle:[nil]image:[UIImage imageNamed:@"white_stats.png"]selectedImage:[UIImage imageNamed:@"white_stats.png"]];

nil isn't an object (it doesn't go in square brackets). Change this to:

[self.tabBarItem initWithTitle:nil image:myImage selectedImage:[UIImage imageNamed:@"white_stats.png"]];

Alternatively, you can technically do this in one line:

[self.tabBarItem initWithTitle:nil image:[[UIImage imageNamed:@"white_stats.png"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal] selectedImage:[UIImage imageNamed:@"white_stats.png"]];

But that is hard to read if you need to come back to it later, so don't do that.