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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18688189/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-31 01:02:19  来源:igfitidea点击:

How to change tab bar item text color

iphoneiosobjective-cios7

提问by Priyatham51

enter image description here

enter image description here

How can I change the color of "More.." text in tabbar to match with its icon color. (Right now Performance is selected in the tab bar)

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

I tried to set TitleTextAttributes.

我试图设置 TitleTextAttributes。

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

But it the text color is always set to yellow. even when the item is selected. Like this enter image description here

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

I am trying set to white when selected and when unselected it should match with icon color. Thanks.. Any suggestions will be really helpful.

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

回答by skywinder

The accepted answer's code not work for me.

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

Here is code, that works:

这是代码,有效:

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

回答by Priyatham51

I found the answer for my own question.

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

We can set perforamceItem setTitleTextAttributes:for two different states.

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

  • forState:UIControlStateNormal
  • forState:UIControlStateHighlighted
  • forState:UIControlStateNormal
  • forState:UIControlStateHighlighted

I added the following code

我添加了以下代码

 [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];

I need to replace the yellow color with the color of my ICONS. This is how they are looking now.

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

When More is selected

选择更多时

When More is selected

When More is selected

When Performance is Selected

选择性能时

When Performance is Selected

When Performance is Selected

回答by HannahCarney

Code free way to do this:

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

If you are just using iOS 10 then you may change the Image Tint in your Tab Bar

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

enter image description here

enter image description here

If you are also supporting iOS 9 and lower, then you must also add tintColor to your user definer runtime attributes in each tab bar item

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

enter image description here

enter image description here

if you also wish to change your icon color make sure the correct color image is in your assest folder and change Render as to Original Image

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

enter image description here

enter image description here

回答by Chathuranga Silva

This is the swift version :-

这是快速版本:-

        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)

        }

Or you can simply change in Appdelegate :-

或者您可以简单地更改 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
}

回答by Baran Emre

Swift 5.1+ iOS 12.4 & iOS 13:

斯威夫特 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)
            }
        }
    }
}

回答by Bruno Paulino

Swift 4:

斯威夫特 4:

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

回答by LHIOUI

Swift version of @skywinder answer :

@skywinder 的 Swift 版本回答:

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

回答by Paul King

For a swift solution, let type inference be your friend:

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

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

回答by PeiweiChen

This is easy , simply subclass UITabBarItem and assign it to be the class of your tab bar item in either storyboard or code. The below works perfectly for me.

这很简单,只需将 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's solution is good but it triggers global scope.

skywinder 的解决方案很好,但它触发了全局范围。

回答by Z?k

This works correctly..

这工作正常..

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

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