Custom UITabBar unselected item's color

5.6k views Asked by At

I am trying to change the default grey colour of the unselected UITabBarItems. I have managed to change the text but not the image.

enter image description here

TabBar.appearance().barTintColor = UIColor(red: 86.0/255.0, green: 132.0/255.0, blue: 208.0/255.0, alpha: 1.0)

var normalTint: UIColor = UIColor.whiteColor()

TabBar.appearance().tintColor = UIColor.whiteColor()

UITabBarItem.appearance().setTitleTextAttributes([NSForegroundColorAttributeName: normalTint,NSFontAttributeName: UIFont(name: "Arial", size: 13)!], forState: UIControlState.Normal)
4

There are 4 answers

0
Özgür Ersil On BEST ANSWER

you can use .AlwaysOriginal

tabBarItem.selectedImage = UIImage(named: "first-selected")!.imageWithRenderingMode(.AlwaysOriginal)
1
Derek Soike On

iOS 10 | Swift 3

class TabBarVC: UITabBarController {

    override func viewDidLoad() {
        super.viewDidLoad()

        // make unselected icons white
        self.tabBar.unselectedItemTintColor = UIColor.white
    }
}
1
raja On
  • If you want to set unselected icons to specific color through Storyboard.
  • You can do it through 'User defined runtime attributes', without adding the codes. enter image description here
0
Amrit Giri On

func styleTabBar(){
    let fontAttributes = [NSAttributedString.Key.foregroundColor: UIColor.gray, NSAttributedString.Key.font: UIFont.systemFont(ofSize: 10.0)]
    let selectedFontAttributes = [NSAttributedString.Key.foregroundColor: UIColor.red, NSAttributedString.Key.font: UIFont.systemFont(ofSize: 10.0)]
    if #available(iOS 13.0, *) {
        let tabBarAppearance = UITabBarAppearance()
        let tabBarItemAppearance = UITabBarItemAppearance()
        tabBarItemAppearance.normal.iconColor = UIColor.gray
        tabBarItemAppearance.selected.iconColor = UIColor.red
        tabBarItemAppearance.normal.titleTextAttributes = fontAttributes
        tabBarItemAppearance.selected.titleTextAttributes = selectedFontAttributes
        
        tabBarAppearance.configureWithOpaqueBackground()
        tabBarAppearance.backgroundColor = UIColor.white
        tabBarAppearance.stackedLayoutAppearance = tabBarItemAppearance
        UITabBar.appearance().standardAppearance = tabBarAppearance
        if #available(iOS 15.0, *) {
            UITabBar.appearance().scrollEdgeAppearance = tabBarAppearance
        }
    }else{
        UITabBar.appearance().barTintColor = UIColor.white
        UITabBar.appearance().tintColor = UIColor.gray
        UITabBar.appearance().unselectedItemTintColor = UIColor.red
     UITabBarItem.appearance().setTitleTextAttributes(fontAttributes, for: .normal)
UITabBarItem.appearance().setTitleTextAttributes(selectedFontAttributes, for: .selected)
    }

}