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.
Asked
Active
Viewed 1,845 times
2
-
Are you using a custom uitabbarcontroller? – Tyler Dec 12 '15 at 03:29
-
So you want to make UITabBar icons to RGB colors? – Twitter khuong291 Dec 12 '15 at 03:34
-
Using the default UITabBarController, and I'm using .png's for my icons. I'd like to keep the original colors of my .png's but Xcode is coloring them to gray. – Grant Park Dec 12 '15 at 05:29
3 Answers
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