3
override func viewDidLoad() {
        super.viewDidLoad()

        // Do any additional setup after loading the view.'
        UITabBar.appearance().tintColor = UIColor.whiteColor()
        UITabBar.appearance().selectedImageTintColor = UIColor.redColor()
    } //method is in TabBarViewController

I would like the unselected color to be white and selected color to be red. The selectedImageTintColor property is deprecated in iOS 9, and I was wondering how I'd be able to change the unselected image color otherwise? Also, I was able to change the selected image tint color the red color in storyboard by changing the tabbar keypath's color attribute to red. I was wondering if there was a way of changing the unselected and selected color in storyboard?

stumped
  • 3,235
  • 7
  • 43
  • 76

3 Answers3

1

Make sure your original image is displayed as white. Then you just tell the TabBarItem to render the unselected image as .AlwaysOriginal and the set a selectedImage on it like so:

let tabBarImage = UIImage(named: "icon-tab-name")
let tabBarItem = UITabBarItem(title: "Title", image: tabBarImage?.imageWithRenderingMode(.AlwaysOriginal), selectedImage: tabBarImage)

And then have

UITabBar.appearance().tintColor = UIColor.redColor()

This way you will have a white unselected state and a red selected state.

Zappel
  • 1,612
  • 1
  • 22
  • 37
0

I did with this peace of code in the viewDidLoad method:

self.tabBar.tintColor = UIColor.whiteColor()
Danny Narváez
  • 576
  • 5
  • 10
  • Sorry, I was unclear in my description. I'd like to have the unselected white and the selected red. When I set the tint color to white (as with your code or variations), it has the selected as red but the unselected as white, – stumped Nov 24 '15 at 01:11
  • I solved a similar issue combining with the UIImage imageWithRenderingMode method. Just like this: `myImage.imageWithRenderingMode(.AlwaysOriginal)` – Danny Narváez Nov 24 '15 at 14:38
0

If you would like to change all of them in IOS 10, do something like this in the appDelegate. This is changing all the unselected ones to black.

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
    // Override point for customization after application launch.

    UITabBar.appearance().unselectedItemTintColor = UIColor(displayP3Red: 0, green: 0, blue: 0, alpha: 1)

    return true
}
thalacker
  • 2,389
  • 3
  • 23
  • 44