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

19

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

未选中状态的默认颜色是浅灰色,但它在我的较暗 UITabBar 背景下不显示。

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

在我的应用程序委托的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]];

3
在iOS 10及以上版本和Swift 3中,您可以使用tabBarCtrl.tabBar.unselectedItemTintColor = UIColor.gray来设置未选中选项卡的灰色颜色。 - Anchor
https://dev59.com/y2Mk5IYBdhLWcg3wvARo#40741444 - Daniel Galasko
4个回答

47

搞定了!

使用这个来改变文字颜色:

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

6
原始图像是一件好东西。感谢您分享。 - VaporwareWolf
4
如果我的原始图标是黑色的,但我想要它变成白色,该怎么办? - Mark Buikema
1
我曾经遇到过类似的情况,但是我想让我的未选中图像为白色(原始图像为白色),而我的选中图像为黄色(目前为灰色)。如果我愿意改变我的选中图像的颜色,我相信他上面的解决方案可以解决问题,但是我想使用内置的“着色颜色”属性来设置选中图像。当我尝试将我的选中图像放在属性检查器下的选项卡栏项目中时,在运行时它会留下空白的图像(错误)。我在我的应用程序委托中使用了这个解决方案:[链接](https://dev59.com/vF8d5IYBdhLWcg3wt0Gy) - Renee Olson
@ReneeOlson 感觉有点不太正规,但确实有效。很高兴能对你有所帮助 :) - SleepsOnNewspapers
无法相信在iOS 8中必须预先渲染色调。这是一个巨大的倒退!但还是谢谢!有没有办法在Storyboard中实现这个功能? - jowie
很遗憾,在iOS 13/XCode 11.x上这个无法工作。 - Nithin

27

在你的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]];

11
更改未选择的色调后,选择该项然后取消选择,未选择的色调将会更改回之前的状态。似乎唯一的方法是使用自己的图像来获得所需的未选择状态颜色。您可以在此处查看如何操作:https://dev59.com/-XjZa4cB1Zd3GeqPfIYq#19662170 - TheSD
我尝试了一下,发现你也可以使用[[UIButton appearance] setTintColor:[UIColor greenColor]];来保持iOS 7/8中选定的标签栏项目和按钮标签之间的一致性(例如像音乐应用程序)。 - Nicolas Miari
运行得很好。谢谢! - Vladislav Kovalyov
不幸的是,这在后续版本中已被弃用。 - Totoro
[[UIView appearanceWhenContainedIn:[UITabBar class], nil] setTintColor:[UIColor redColor]]; 已被弃用且不起作用。请注意。 - coolcool1994

14
你可以在不编写任何代码的情况下,通过资源文件属性检查器将图像呈现为原始状态。 在此输入图片描述

1
不是来自故事板,而是来自资源文件的属性检查器。除此之外,这是这里最好的答案。 - Marmoy

0

你也可以直接在Storyboard中设置... 在这里查看我的答案: 如何设置UITabBarItem的未选中色调,包括系统项目(iOS7)

如果你正在使用Storyboard,你也可以同时设置Bar ItemImageSelected Bar ItemSelected Image,以在tabBar中获得未更改的图像。

或者在Assets目录中,您可以选择Render As: Original Image在您的图像属性中(View > Utilities > Show Attributes Inspector或快捷键⌥⌘4(Option + Command + 4))


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接