2

Can anyone tell me in Swift 2.0 how to override Xcode's default gray color in my UITabBar icons? This question's not cutting it for me: Tab bar item icons appear darker.

Community
  • 1
  • 1
Grant Park
  • 1,004
  • 1
  • 9
  • 29

3 Answers3

6

In my first view controller's viewDidLoad, I placed the following and it worked like a charm:

    let aTabArray: [UITabBarItem] = (self.tabBarController?.tabBar.items)!

    for item in aTabArray {
        item.image = item.image?.imageWithRenderingMode(UIImageRenderingMode.AlwaysOriginal)
        item.imageInsets = UIEdgeInsetsMake(7, 0, -7, 0)
    }
Grant Park
  • 1,004
  • 1
  • 9
  • 29
0

you need to set color for UITabBarItem or else you have to set In UITabBarItem along with along with you need to set imageInsets for UITabBarItem like UITabBarItem is

let customTabBarItem:UITabBarItem = UITabBarItem(title: nil, image: UIImage(named: "favorites.png")?.imageWithRenderingMode(UIImageRenderingMode.AlwaysOriginal), selectedImage: UIImage(named: "favorites_h.png")?.imageWithRenderingMode(UIImageRenderingMode.AlwaysOriginal))

and imageInset :

customTabBarItem.imageInsets = UIEdgeInsetsMake(5, 0, -5, 0)
Shubham bairagi
  • 943
  • 7
  • 25
0

For iOS13+, I use code below to set UITabBar item title && icon color to support dark mode.

       if #available(iOS 13, *) {
            let appearance = UITabBarAppearance()
            appearance.shadowImage = UIImage()
            appearance.shadowColor = .white
            
            appearance.stackedLayoutAppearance.normal.iconColor = .label
            appearance.stackedLayoutAppearance.normal.titleTextAttributes = [NSAttributedString.Key.foregroundColor: UIColor.label]
            //            appearance.stackedLayoutAppearance.normal.badgeBackgroundColor = .yellow
            
            appearance.stackedLayoutAppearance.selected.iconColor = .myGreen
            appearance.stackedLayoutAppearance.selected.titleTextAttributes = [NSAttributedString.Key.foregroundColor: UIColor.myGreen]
            self.tabBar.standardAppearance = appearance
        }
Zhou Haibo
  • 1,681
  • 1
  • 12
  • 32