stumped
stumped

Reputation: 3293

Changing the unselected color of a UITabBar Item iOS 9

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?

Upvotes: 3

Views: 2950

Answers (3)

thalacker
thalacker

Reputation: 2837

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
}

Upvotes: 0

Zappel
Zappel

Reputation: 1751

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.

Upvotes: 1

Danny Narváez
Danny Narváez

Reputation: 586

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

self.tabBar.tintColor = UIColor.whiteColor()

Upvotes: 0

Related Questions