如何更改UITabBarselect颜色

我需要将UITabBar的select颜色从默认的蓝色更改为红色。 我们如何做到这一点。

2017年9月更新:自从我写了这个答案已经两年了,由于它定期收到upvotes,我应该说这可能是这个问题最糟糕的答案,它很容易出错,可能会因iOS更新而中断,很难debugging等,所以请不要做我写的东西,并应用更好的解决scheme,如UITabBar或UITabBarController的子类。 谢谢。

你可以通过为你的UITabBar设置一个“tintColor”属性(Key Path)。

  1. select文档大纲中的UITabBar。 ( 不是带有黄色图标的控制器。)
  2. 在“实用程序”区域中select“标识检查器”。
  3. 单击“用户定义的运行属性”中的+。
  4. 添加“色彩”types的“tintColor”键path和所需的颜色。

这应该做到这一点。 你可以根据下面的截图来检查它。

在这里输入图像说明

更多关于这个: UITabBar的Identity Inspector中有一个“Tint”属性,我相信它会做同样的事情,但是显然它什么都不做。 默认值是selectUITabBarItem时的默认填充颜色,所以我的猜测是在稳定版本Xcode 7中修复。手指交叉。

在IOS5中,UITabBar有一个selectedImageTintColor属性,它可以满足你的需求。

在iOS 7中,它只是tintColor。 一种方法可以是子类UITabBarViewController,在故事板中设置自定义类,并在子类tabBarVC的viewDidLoad方法中添加:

 [[self tabBar] setTintColor:[UIColor redColor]]; 

这是非常容易的

创build一个自定义类的UITabBarController,并在-(void)viewDidLoad方法中添加这一行:

 [[self tabBar] setSelectedImageTintColor:[UIColor greenColor]]; 

由于UITextAttributeTextColor在iOS 7中已被弃用,因此您应该使用:

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

SDK不能使这个简单,但在技术上是可行的。 苹果公司显然认为这是他们一致的外观和感觉的一部分。

UITabBar是UIView的子类。 你总是可以-drawRect:和实现你自己的-drawRect:

这不是一个小任务,但是,你必须从头开始重新实现这个类,否则你冒着一些奇怪的副作用。

只需在TabBar的Interface Builder中更改以下属性即可

在我的情况下显然是白色的。

我一直在寻找一种方法来设置UITabBarItem的选定文本颜色,并使用UIAppearance协议find了一个简单的方法。

 [UITabBarItem.appearance setTitleTextAttributes:@{ UITextAttributeTextColor : [UIColor greenColor] } forState:UIControlStateNormal]; [UITabBarItem.appearance setTitleTextAttributes:@{ UITextAttributeTextColor : [UIColor purpleColor] } forState:UIControlStateSelected]; 

请原谅可怕的颜色!

从iOS 8开始,它非常简单:

 UITabBar.appearance().tintColor = UIColor.redColor() 

iOS 5.0修复了这个问题,但解决scheme是在NDA下。 在您的文档中查找UITabBar,以简单的方式执行您想要的操作。

在这里输入图像说明

要达到上述效果,请执行以下步骤。

第1步:Assets.xcassets添加所需的图像,并确保它们Render AsDefault

在这里输入图像说明

第2步:select你的UITabBar对象并设置Image Tint颜色,这个颜色将被选中标签颜色

在这里输入图像说明

第3步:selectUITabBar对象并添加关键pathunselectedItemTintColortypesColorChoose color for unselected item 用户定义的运行属性中的 Choose color for unselected item

在这里输入图像说明

全做完了。