2009-04-26 126 views

回答

4

这个SDK并没有使这个简单,但它是在技术上可能。苹果显然认为这是他们一致观感的一部分。

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

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

1

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

18

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

+4

在iOS 8中弃用。 – KPM 2014-09-25 20:18:11

+1

@KPM,我发现它已被弃用,但我们应该用什么来代替?这仍然适用于iOS 8,因为它值得。 – 2014-09-28 22:10:39

+1

查看下面的凯尔的答案。 http://stackoverflow.com/a/18996564/364446 – KPM 2014-09-28 22:16:50

11

这是非常容易

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

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

但这会改变文本的颜色,而不是整个选择区域。任何方式来实现这一目标? – 2015-02-09 05:53:37

+3

已弃用ios 8 – codercat 2015-07-30 13:00:20

3

我一直在寻找一种方式来设置所选文本颜色一个UITabBarItem,并且使用UIAppearance协议发现了一个死的简单方法。

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

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

请原谅可怕的颜色!

+0

已弃用UITextAttributeTextColor。最新的选择是什么? – 2017-04-25 07:28:04

14

在iOS 7中,它只是tintColor。做到这一点的一种方法是子类UITabBarViewController,设置在故事板自定义类,并在子类的tabBarVC的viewDidLoad方法补充一点:

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

因为UITextAttributeTextColor在iOS的7弃用,你应该使用:

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

更新2017年9月: 它已经两年了,因为我已经写了这个答案,因为它是定期接收upvotes,我应该说,这可能是最糟糕的回答这个问题,这是容易出错,里因为iOS更新而难以调试等等,所以请不要执行我写的东西,并应用更好的解决方案,例如UITabBar或UITabBarController的子类化。谢谢。

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

  1. 在文档大纲中选择UITabBar。 (不是带有黄色图标的控制器。)
  2. 在Utilities区域中选择Identity Inspector。
  3. 单击“用户定义的运行属性”中的+。
  4. 添加类型为“Color”的“tintColor”键路径和所需的颜色。

这应该这样做。您可以根据下面的屏幕截图进行检查。

enter image description here

更多内容: 有一个在UITabBar的身份检查,我相信会做同样的事情,但很显然,它什么都不做的“色调”属性。它的默认值是选择UITabBarItem时的确切默认填充颜色,所以我的猜测是它会在稳定版本Xcode 7中修复。手指交叉。

2

从iOS的8起它的那样简单:

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

只要在我的情况下,其白更改以下属性在Interface Builder为的TabBar

明显。

1
enter image description here

为了达到上述结果来执行以下步骤。

第1步:添加你想要的图像Assets.xcassets,并确保他们Render AsDefault

enter image description here

第2步:选择UITabBar对象,并设置Image Tint颜色,这种颜色会选标签颜色

enter image description here

第3步:选择UITabBar对象,并添加关键路径unselectedItemTintColor类型ColorChoose color for unselected item用户自定义属性运行

enter image description here

全部完成。