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

时间:2013-09-08 20:11:40

标签: iphone ios objective-c ios7

enter image description here

如何更改标签栏中“更多..”文字的颜色以与其图标颜色相匹配。 (现在在选项卡栏中选择了Performance)

我尝试设置TitleTextAttributes。

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

但是文本颜色总是设置为黄色。即使选择了该项目。像这样 enter image description here

我尝试在选中时设置为白色,未选中时应与图标颜色匹配。 谢谢.. 任何建议都会非常有用。

12 个答案:

答案 0 :(得分:50)

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

以下是适用的代码:

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

答案 1 :(得分:17)

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

我们可以为两种不同的状态设置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];

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

选择更多时

When More is selected

选择效果时

When Performance is Selected

答案 2 :(得分:7)

无代码方式:

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

enter image description here

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

enter image description here

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

enter image description here

答案 3 :(得分:6)

这是快速版本: -

        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
}

答案 4 :(得分:3)

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

答案 5 :(得分:2)

斯威夫特4:

UITabBarItem.appearance().setTitleTextAttributes([NSAttributedStringKey.foregroundColor : UIColor.white], for: .normal)
UITabBarItem.appearance().setTitleTextAttributes([NSAttributedStringKey.foregroundColor : UIColor.red], for: .selected)

答案 6 :(得分:1)

对于快速解决方案,让类型推断成为你的朋友:

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

答案 7 :(得分:1)

这很简单,只是子类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的解决方案很好,但它会触发全球范围。

答案 8 :(得分:1)

Swift版本的@skywinder回答:

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

答案 9 :(得分:0)

这可以正常工作..

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

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

答案 10 :(得分:0)

这在Swift 5上对我有用。

在AppDelegate中:

 UITabBarItem.appearance().setTitleTextAttributes([NSAttributedString.Key.foregroundColor : UIColor.red], for: .selected)

答案 11 :(得分:0)

如今,如果您的应用支持的iOS版本低于13,则应以其他方式设置此颜色:

if #available(iOS 13, *) {
    let appearance = UITabBarAppearance()
    appearance.stackedLayoutAppearance.selected.titleTextAttributes = [NSAttributedString.Key.foregroundColor: .red]
    tabBar.standardAppearance = appearance
} else {
    UITabBarItem.appearance().setTitleTextAttributes(UIColor.red, for: UIControl.State.selected)
}

在代码示例中,我为UITabBarItem的选定状态设置了红色文本颜色,您可能还需要为正常状态更改文本颜色。