Changing tab bar item image and text color iOS

前端 未结 22 1040
说谎
说谎 2020-12-02 06:10

Here is my tab bar:

\"enter

The following image shows the program being run an

相关标签:
22条回答
  • 2020-12-02 06:52

    you can set tintColor of UIBarItem :

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

    Swift 3

    This worked for me (referring to set tabBarItems image colors):

    UITabBar.appearance().tintColor = ThemeColor.Blue
    
    if let items = tabBarController.tabBar.items {
            let tabBarImages = getTabBarImages() // tabBarImages: [UIImage]
            for i in 0..<items.count {
                let tabBarItem = items[i]
                let tabBarImage = tabBarImages[i]
                tabBarItem.image = tabBarImage.withRenderingMode(.alwaysOriginal)
                tabBarItem.selectedImage = tabBarImage
            }
        }
    

    I have noticed that if you set image with rendering mode = .alwaysOriginal, the UITabBar.tintColor doesn't have any effect.

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

    You may also do by this way:

    override func viewWillLayoutSubviews() {  
      if let items = self.tabBar.items {
        for item in 0..<items.count {
          items[item].image = items[item].image?.withRenderingMode(.alwaysOriginal)
                items[item].selectedImage = items[item].selectedImage?.withRenderingMode(.alwaysTemplate)
        }
    

    Optional:

     UITabBar.appearance().tintColor = UIColor.red
    

    I hope it will help you.

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

    Try add it on AppDelegate.swift (inside application method):

    UITabBar.appearance().tintColor = UIColor(red: 0/255.0, green: 0/255.0, blue: 0/255.0, alpha: 1.0)
    
    // For WHITE color: 
    UITabBar.appearance().tintColor = UIColor(red: 255/255.0, green: 255/255.0, blue: 255/255.0, alpha: 1.0)
    

    Example:

    func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
        // Tab bar icon selected color
        UITabBar.appearance().tintColor = UIColor(red: 0/255.0, green: 0/255.0, blue: 0/255.0, alpha: 1.0)
        // For WHITE color: UITabBar.appearance().tintColor = UIColor(red: 255/255.0, green: 255/255.0, blue: 255/255.0, alpha: 1.0)
        return true
    }
    

    Example:

    My english is so bad! I'm sorry! :-)

    0 讨论(0)
  • 2020-12-02 07:00

    Swift 4: In your UITabBarController change it by this code

    tabBar.unselectedItemTintColor = .black
    
    0 讨论(0)
  • 2020-12-02 07:01

    Swift


    For Image:

    custom.tabBarItem = UITabBarItem(title: "Home", image: UIImage(named: "tab_icon_normal"), selectedImage: UIImage(named: "tab_icon_selected"))
    

    For Text:

    UITabBarItem.appearance().setTitleTextAttributes([NSAttributedString.Key.foregroundColor: UIColor.gray], for: .normal)
        
    UITabBarItem.appearance().setTitleTextAttributes([NSAttributedString.Key.foregroundColor: UIColor.red], for: .selected)
    
    0 讨论(0)
提交回复
热议问题