如何更改标签栏项目的文字颜色

在这里输入图像说明

如何更改标签栏中“更多..”文本的颜色以匹配其图标颜色。 (现在在性能被选中标签栏)

我试图设置TitleTextAttributes。

[moreItem setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:[UIFont fontWithName:@"HelveticaLTStd-Roman" size:10.0f], NSFontAttributeName, [UIColor yellowColor],NSForegroundColorAttributeName , nil] 

但它的文字颜色总是被设置为黄色。 即使该项目被选中。 喜欢这个 在这里输入图像说明

我正在尝试设置为白色时,select时,它应该与图标颜色匹配。 谢谢..任何build议将是非常有益的。

接受的答案的代码不适合我。

这是代码,工作:

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

我为自己的问题find了答案。

我们可以设置perforamceItem setTitleTextAttributes:两种不同的状态。

  • forState:UIControlStateNormal
  • forState:UIControlStateHighlighted

我添加了下面的代码

  [performanceItem setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:[UIFont fontWithName:@"HelveticaLTStd-Roman" size:10.0f], NSFontAttributeName, [UIColor yellowColor], NSForegroundColorAttributeName,nil] forState:UIControlStateNormal]; [performanceItem setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:[UIFont fontWithName:@"HelveticaLTStd-Roman" size:10.0f], NSFontAttributeName, [UIColor whiteColor], NSForegroundColorAttributeName,nil] forState:UIControlStateHighlighted]; 

我需要用我的图标颜色replace黄色。 这就是他们现在的样子。

当select更多时

当选择更多时

select性能时

选择性能时

这是迅速的版本: –

  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) } 

或者你可以简单地改变Appdelegate: –

  func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool { UITabBarItem.appearance().setTitleTextAttributes([NSForegroundColorAttributeName: UIColor.blueColor()], forState: .Selected) UITabBarItem.appearance().setTitleTextAttributes([NSForegroundColorAttributeName: UIColor.blackColor()], forState: .Normal) // Override point for customization after application launch. return true } 

无代码的方式来做到这一点:

如果你只是使用iOS 10,那么你可以改变你的标签栏中的图像色调

在这里输入图像说明

如果您还支持iOS 9或更低版本,则还必须将tintColor添加到每个选项卡栏项目中的用户定义器运行时属性

在这里输入图像说明

如果您还想更改图标颜色,请确保正确的彩色图像位于您的声明文件夹中,并将渲染更改为原始图像

在这里输入图像说明

为了迅速解决,让types推断成为你的朋友:

 override func viewWillAppear(animated: Bool) { for item in self.tabBar.items! { let unselectedItem = [NSForegroundColorAttributeName: UIColor.blackColor()] let selectedItem = [NSForegroundColorAttributeName: UIColor.whiteColor()] item.setTitleTextAttributes(unselectedItem, forState: .Normal) item.setTitleTextAttributes(selectedItem, forState: .Selected) } } 

这很简单,只需将UITabBarItem的子类转换为故事板或代码中标签栏项的类即可。 下面的作品完全适合我。

 import UIKit class PPTabBarItem: UITabBarItem { required init?(coder aDecoder: NSCoder) { super.init(coder: aDecoder) commonInit() } override init() { super.init() commonInit() } func commonInit() { self.setTitleTextAttributes([NSFontAttributeName: UIFont.systemFontOfSize(13), NSForegroundColorAttributeName:UIColor.blackColor()], forState: UIControlState.Normal) self.setTitleTextAttributes([NSFontAttributeName: UIFont.systemFontOfSize(13), NSForegroundColorAttributeName:UIColor.yellowColor()], forState: UIControlState.Selected) } } 

skywinder的解决scheme是好的,但它触发全球范围。

@skywinder的swift版本回答:

 UITabBarItem.appearance().setTitleTextAttributes([NSForegroundColorAttributeName:UIColor.whiteColor()], forState: .Normal) UITabBarItem.appearance().setTitleTextAttributes([NSForegroundColorAttributeName:UIColor.whiteColor()], forState: .Selected) 

这工作正常..

  [[UITabBarItem appearance] setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys: [UIColor redColor], NSForegroundColorAttributeName, nil] forState:UIControlStateSelected]; [[UITabBarItem appearance] setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys: [UIColor blackColor], NSForegroundColorAttributeName, nil] forState:UIControlStateNormal];