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

Pri*_*m51 15 iphone objective-c ios ios7

在此输入图像描述

如何更改标签栏中"更多..."文本的颜色以匹配其图标颜色.(现在在标签栏中选择了Performance)

我试着设置TitleTextAttributes.

[moreItem setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:[UIFont fontWithName:@"HelveticaLTStd-Roman" size:10.0f], NSFontAttributeName,  [UIColor yellowColor],NSForegroundColorAttributeName , nil]
Run Code Online (Sandbox Code Playgroud)

但它的文字颜色总是设置为黄色.即使选择了该项目.像这样 在此输入图像描述

我选择设置为白色,未选中时应与图标颜色匹配.谢谢..任何建议都会非常有帮助.

sky*_*der 51

接受的答案代码对我不起作用.

这是代码,有效:

    [[UITabBarItem appearance] setTitleTextAttributes:@{ NSForegroundColorAttributeName : [UIColor yellowColor] }
                                             forState:UIControlStateNormal];
    [[UITabBarItem appearance] setTitleTextAttributes:@{ NSForegroundColorAttributeName : [UIColor whiteColor] }
                                             forState:UIControlStateSelected];
Run Code Online (Sandbox Code Playgroud)


Pri*_*m51 18

我找到了自己问题的答案.

我们可以设置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];
Run Code Online (Sandbox Code Playgroud)

我需要用我的图标颜色替换黄色.这就是他们现在的样子.

选择更多时

选择更多时

选择性能时

选择性能时


Bar*_*mre 8

斯威夫特 5.1 + iOS 12.4 和 iOS 13

/// Subclass of `UITabBarController` that is used to set certain text colors for tab bar items.
class TabBarController: UITabBarController {

    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)
        if let items = tabBar.items {
            // Setting the title text color of all tab bar items:
            for item in items {
                item.setTitleTextAttributes([.foregroundColor: UIColor.black], for: .selected)
                item.setTitleTextAttributes([.foregroundColor: UIColor.lightGray], for: .normal)
            }
        }
    }
}
Run Code Online (Sandbox Code Playgroud)


Cha*_*lva 7

这是迅捷版: -

        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)

        }
Run Code Online (Sandbox Code Playgroud)

或者你可以简单地改变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
}
Run Code Online (Sandbox Code Playgroud)


Han*_*ney 7

无代码方式执行此操作:

如果您只是使用iOS 10,则可以更改选项卡栏中的图像色调

在此输入图像描述

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

在此输入图像描述

如果您还想更改图标颜色,请确保您的assest文件夹中的颜色图像正确,并将Render更改为原始图像

在此输入图像描述