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

时间:2014-10-24 15:44:50

标签: ios ios8 uitabbaritem tintcolor bartintcolor

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

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

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

在我的app委托中,didfinishlaunchingwithoptions:我有

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

4 个答案:

答案 0 :(得分:46)

想出来了!

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

[[UITabBarItem appearance] setTitleTextAttributes:@{ NSForegroundColorAttributeName : [UIColor greenColor] }
                                         forState:UIControlStateNormal];
[[UITabBarItem appearance] setTitleTextAttributes:@{ NSForegroundColorAttributeName : [UIColor blackColor] }
                                         forState:UIControlStateSelected];

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

UIImage *deselectedImage = [[UIImage imageNamed:@"deselectedImage"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
UIImage *selectedImage = [[UIImage imageNamed:@"selectedImage"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];

答案 1 :(得分:22)

application didFinishLaunchingWithOptions:内的AppDelegate.m中,使用以下代码:

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

答案 2 :(得分:14)

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

enter image description here

答案 3 :(得分:0)

您也可以直接在故事板中进行设置...在此处查看我的答案: How to set UITabBarItem's unselected tint, ***including system items*** (iOS7)

  

如果您正在使用Storyboard,则还可以为Bar Item设置Selected Image,为Selected Bar Item设置Assets,以便在tabBar中获得未更改的图片。

或者在Render As目录中,您可以在图片的属性中选择Original ImageViewUtilities> Show Attributes Inspector> {{1 }或快捷方式⌥⌘4(Option + Command + 4))