2012-07-05 66 views
4

我已经编程创建了一个tabbar。更改tabbar文本颜色,iPhone

我们可以改变tabbar项目的标题颜色吗? 默认为白色,我试图使它变黑。

tabbaritem.text.textcolor=[UIcolor Blackcolor]; 

感谢

+0

'[的UIColor Blackcolor]'是不是UIColor'的'的方法,它是'UIColor'我想你彪'[的UIColor blackcolor]' – rckoenes 2012-07-05 12:11:45

回答

31

在iOS5的使用外观代理更改标题颜色:

对于一个特定的TabBar项:

[[UITabBarItem appearance] setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys: 
             [UIFont fontWithName:@"AmericanTypewriter" size:20.0f], UITextAttributeFont, 
             [UIColor yellowColor], UITextAttributeTextColor, 
             [UIColor redColor], UITextAttributeTextShadowColor, 
             [NSValue valueWithUIOffset:UIOffsetMake(0.0f, 1.0f)], UITextAttributeTextShadowOffset, 
             nil] forState:UIControlStateNormal]; 

或更换self.tabBarItem[UITabBarItem appearance]改变所有y我们的标签栏项目。 如果你想改变他们,我建议你把代码上的appdelegate didFinishLaunchingWithOptions:

+0

应用不要崩溃了,但文本的颜色不会改变。任何想法 – FirstTimer 2012-07-11 09:09:13

+0

我已经更新了代码,并在使用UITabBarController的项目中对其进行了测试,并且它可以工作。 – rckoenes 2012-07-11 09:21:25

+0

现在它确实有效....感谢一吨 – FirstTimer 2012-07-11 10:13:34

2

只是别的东西......

要设置标准外观的iOS 5:

在你AppDelegate.m做:

[[UITabBar appearance] setTintColor:myColor]; //or whatever you want to change 

为您节省很多工作。

+1

你的意思是[[UITabBar appearance] setTintColor:myColor]; – 2013-11-20 22:28:17

+0

正常工作... – 2015-01-03 10:03:04

0

也许这个更新的代码可以帮助别人:

[UITabBarItem.appearance setTitleTextAttributes:@{ 
                NSForegroundColorAttributeName : [UIColor lightGrayColor] } forState:UIControlStateNormal]; 

[UITabBarItem.appearance setTitleTextAttributes:@{ 
                NSForegroundColorAttributeName : [UIColor whiteColor] }  forState:UIControlStateSelected];