UItabbar settitlecolor,iPhone

时间:2012-07-09 09:08:26

标签: iphone ipad

我正在尝试以编程方式更改tabbar项目文本的颜色。 我正在使用

[[UITabBar appearance] setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:
                                                       [UIFont fontWithName:@"AmericanTypewriter" size:20.0f], UITextAttributeFont,
                                                       [UIColor blackColor], UITextAttributeTextColor,
                                                       [UIColor grayColor], UITextAttributeTextShadowColor,
                                                       [NSValue valueWithUIOffset:UIOffsetMake(0.0f, 1.0f)], UITextAttributeTextShadowOffset,
                                                       nil]];

哪个适用于iOS5及以上版本。 但是我的应用程序在控制台上因错误而崩溃:

*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[_UIAppearance setTitleTextAttributes:]: unrecognized selector sent to instance 0x79f5790'
*** First throw call stack:

不确定,为什么我会崩溃。 如果还有其他方法可以更改tabbar项的字体颜色,请另外建议。

由于

3 个答案:

答案 0 :(得分:2)

这些答案都不正确(至少对iOS 6而言)。

在通话结束时你遗漏了forState:。 实施例

[[UITabBarItem appearance] setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:
                                                       [UIColor whiteColor], UITextAttributeTextColor,
                                                       [UIColor grayColor], UITextAttributeTextShadowColor,
                                                       [NSValue valueWithUIOffset:UIOffsetMake(0.0f, 1.0f)], UITextAttributeTextShadowOffset,
                                                       nil] forState:UIControlStateNormal];

答案 1 :(得分:1)

标题不属于UITabBarUITabBarItem因此取代UITabBar

[[UITabBarItem appearance] setTitleTextAttributes:

答案 2 :(得分:1)

setTitleTextAttributesUIBarItem类的方法。

这样获取标签栏项目并设置其标题属性。

看到这个

tutorial

用于设置标签栏中的样式