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