更改iPhone的选项卡文本颜色

4
我已经通过编程创建了一个选项卡栏。
我们能否更改选项卡项目标题的颜色? 默认是白色,我正在尝试将其改为黑色。
类似于:
 tabbaritem.text.textcolor=[UIcolor Blackcolor];

谢谢


[UIcolor Blackcolor] 不是 UIColor 的一个方法,我想你可能指的是 [UIColor blackcolor] - rckoenes
3个回答

31

在 iOS5 中,您可以使用外观代理(appearance proxy)来更改标题颜色:

对于特定的选项卡条目:

[[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]以更改所有选项卡栏项目。 如果您想要更改它们所有,我建议您将代码放置在应用程序委托的didFinishLaunchingWithOptions:中。


应用程序不再崩溃,但文本的颜色没有改变。有什么想法? - FirstTimer
我已经更新了代码并在使用UITabBarController的项目中进行了测试,它确实可以工作。 - rckoenes
只有一个快速的问题:我能否将文本向上/向下移动? - FirstTimer

2

只是另外一些东西...

在iOS 5中设置您的标准外观:

请在您的AppDelegate.m文件中执行以下操作:

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

可以省去很多工作。


1
你的意思是 [[UITabBar appearance] setTintColor:myColor];。 - Mazen Kasser

0

也许这个更新的代码能帮助到某些人:

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

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

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