更改未选中的UITabBarController项目标题和背景图像的tintColor

Sle*_*ers 17 uitabbaritem tintcolor ios bartintcolor ios8

如何更改未选中的UITabBarItem标题和背景图像iOS 8的tintColor?

未选择状态的默认颜色为浅灰色,但在我的暗色UITabBar背景上不显示

我希望我的未选择状态有[UIColor blackColor]的颜色

在我的应用程序委托内部完成了完成任务:我有

UIImage *deselectedE = [[UIImage imageNamed:@"mincraft_axe_green_32.png"] imageWithRenderingMode:UIImageRenderingModeAutomatic];
UIImage *selectedE = [[UIImage imageNamed:@"mincraft_axe_green_32.png"] imageWithRenderingMode:UIImageRenderingModeAutomatic];
e.tabBarItem =  [[UITabBarItem alloc] initWithTitle:@"Profile" image:deselectedE selectedImage:selectedE];
[[UITabBar appearance] setTintColor:[UIColor blackColor]];
Run Code Online (Sandbox Code Playgroud)

Sle*_*ers 47

弄清楚了!

使用它来更改文本的颜色:

[[UITabBarItem appearance] setTitleTextAttributes:@{ NSForegroundColorAttributeName : [UIColor greenColor] }
                                         forState:UIControlStateNormal];
[[UITabBarItem appearance] setTitleTextAttributes:@{ NSForegroundColorAttributeName : [UIColor blackColor] }
                                         forState:UIControlStateSelected];
Run Code Online (Sandbox Code Playgroud)

并确保图像的图像渲染模式设置为ORIGINAL

UIImage *deselectedImage = [[UIImage imageNamed:@"deselectedImage"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
UIImage *selectedImage = [[UIImage imageNamed:@"selectedImage"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
Run Code Online (Sandbox Code Playgroud)

  • 原始图像是一件好事.感谢分享. (6认同)
  • 如果我的原始图标是黑色但我希望它是白色怎么办? (4认同)

The*_*eSD 24

在您的AppDelegate.m里面application didFinishLaunchingWithOptions:使用以下代码:

//unselected icon tint color 
[[UIView appearanceWhenContainedIn:[UITabBar class], nil] setTintColor:[UIColor redColor]];

//selected tint color 
[[UITabBar appearance] setTintColor:[UIColor greenColor]];

//text tint color 
[[UITabBarItem appearance] setTitleTextAttributes:@{ NSForegroundColorAttributeName : [UIColor whiteColor] }
                                     forState:UIControlStateNormal];

//background tint color 
[[UITabBar appearance] setBarTintColor:[UIColor blueColor]];
Run Code Online (Sandbox Code Playgroud)

  • 更改未选择的色调颜色会在选择该项目后再更改,然后取消选择.似乎将未选择状态变为您想要的颜色的唯一方法是使用您自己的图像.您可以在此处查看如何执行此操作:http://stackoverflow.com/a/19662170/4114683 (11认同)

Bha*_*ain 14

您还可以从属性检查器中将图像渲染为原始图像,而无需编写任何代码

在此输入图像描述