How to change tab bar item text color

前端 未结 12 1933
没有蜡笔的小新
没有蜡笔的小新 2020-12-13 05:36

\"enter

How can I change the color of \"More..\" text in tabbar to match with its icon

相关标签:
12条回答
  • 2020-12-13 05:59

    Swift version of @skywinder answer :

    UITabBarItem.appearance().setTitleTextAttributes([NSForegroundColorAttributeName:UIColor.whiteColor()], forState: .Normal)
    UITabBarItem.appearance().setTitleTextAttributes([NSForegroundColorAttributeName:UIColor.whiteColor()], forState: .Selected)
    
    0 讨论(0)
  • 2020-12-13 06:02

    I found the answer for my own question.

    We can set perforamceItem setTitleTextAttributes: for two different states.

    • 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 Performance is Selected

    When Performance is Selected

    0 讨论(0)
  • 2020-12-13 06:02

    Swift 4:

    UITabBarItem.appearance().setTitleTextAttributes([NSAttributedStringKey.foregroundColor : UIColor.white], for: .normal)
    UITabBarItem.appearance().setTitleTextAttributes([NSAttributedStringKey.foregroundColor : UIColor.red], for: .selected)
    
    0 讨论(0)
  • 2020-12-13 06:02

    This worked for me on Swift 5.

    In AppDelegate :

     UITabBarItem.appearance().setTitleTextAttributes([NSAttributedString.Key.foregroundColor : UIColor.red], for: .selected)
    
    0 讨论(0)
  • 2020-12-13 06:03

    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)
      }
    }
    
    0 讨论(0)
  • 2020-12-13 06:12

    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];
    
    0 讨论(0)
提交回复
热议问题