在所选标签栏上设置文本颜色项目
如果我正确理解您的问题,您想要自定义UITabBarItem
上的文字颜色。不幸的是,它确实不那么灵活。如果你打算这样做(除非你已经在专业人员的帮助下仔细考虑了设计,否则我不推荐!),你必须做一些非常可怕的事情才能实现它。
我建议遍历UITabBar
的子视图(尽可能多的级别),并寻找UILabel
对象。如果你发现一些,你可以改变它们的颜色。如果你不这样做,这意味着它的实现方式不同(可能在某个地方的-drawRect:
方法中);如果发生这种情况,你真的应该放弃。
祝你好运,无论你决定做什么。
也就是说可以通过-drawRect:
,但这样做你的应用程序中,你是高度增加机会通过App Store的
什么? Drawrect是布局UIView的标准方法。 – quantumpotato 2014-08-08 16:02:42
被拒绝使用UIAppearance协议(的iOS5 +),这是目前可能的,而且实际上是相当容易的。
[UITabBarItem.appearance setTitleTextAttributes:@{
UITextAttributeTextColor : [UIColor greenColor] } forState:UIControlStateNormal];
[UITabBarItem.appearance setTitleTextAttributes:@{
UITextAttributeTextColor : [UIColor purpleColor] } forState:UIControlStateSelected];
请原谅可怕的颜色!
这是最终为我工作:
1)选定的文本颜色
[[UIView appearance] setTintColor:someColor];
2)未选择的文本(也改变图像色彩)
[[UITabBar appearance] setTintColor:anotherColor];
只是为了澄清事实有点...
如果您想要更改所有标签栏项目的外观,请使用:
Objective-C的:
[[UITabBarItem appearance] setTitleTextAttributes:@{UITextAttributeTextColor :[UIColor someColor]} forState:UIControlStateSelected];
斯威夫特:
UITabBarItem.appearance().setTitleTextAttributes([NSForegroundColorAttributeName: UIColor.someColor()], forState: .Selected)
然而,如果你只是想设置单个项目的外观做它像这样:
Objective-C:
[self.tabBarItem setTitleTextAttributes:@{UITextAttributeTextColor :[UIColor someColor]} forState:UIControlStateSelected];
斯威夫特:
tabBarItem.setTitleTextAttributes([NSForegroundColorAttributeName: UIColor.someColor()], forState: .Selected)
注意:tabBarItem
是UIViewController
的属性。这意味着虽然每个UIViewController
都有此属性,但它可能不是您要查找的tabBarItem
。当您的视图控制器包含在UINavigationController
中时,通常会出现这种情况。在这种情况下,请访问导航控制器上的tabBarItem
而不是其根目录(或其他)视图控制器中的那个。
这是SWIFT版本: -
for item in self.mainTabBar.items! {
let unselectedItem: NSDictionary = [NSForegroundColorAttributeName: UIColor.whiteColor()]
let selectedItem: NSDictionary = [NSForegroundColorAttributeName: UIColor.whiteColor()]
item.setTitleTextAttributes(unselectedItem as? [String : AnyObject], forState: .Normal)
item.setTitleTextAttributes(selectedItem as? [String : AnyObject], forState: .Selected)
}
@乔纳森英镑......感谢花花公子......我会尝试 – 2010-12-04 09:51:07
没问题! :-) – 2010-12-04 09:57:03