How to change tab bar item text color

前端 未结 12 1934
没有蜡笔的小新
没有蜡笔的小新 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 06:17

    Nowadays if your app supports version of iOS less than 13, you should set this colours different ways:

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

    In code example I set red text color for selected states of UITabBarItem, you may also need to change text color for normal state.

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

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

    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 :-

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

    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.

    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.

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

    Code free way to do this:

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

    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

    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

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

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