dcgoss
dcgoss

Reputation: 2207

Changing UITabBarItem image

I have used Storyboard to set up my UITabBarController and its corresponding ViewControllers. Whenever a tab is deselected, it is gray, when it is selected it has a green tint. I would like one of these UITabBarItems to always look the same: i.e. it always has the green tint regardless of whether it is selected or deselected.
In addition, the icon image I am using for this UITabBarItem already has the green look that I want. This is important because I have tried using this method in the viewDidLoad function of the ViewController of the UITabBarItem that I would like to stay the same (I had already set an outlet between the UITabBarItem in Storyboard and the ViewController):

myTabBarItem.image = UIImage(named: "PickleTabIcon").imageWithRenderingMode(UIImageRenderingMode.AlwaysOriginal)

This works great, however it is not activated until the ViewController is loaded, so a user has to tap on the TabItem to load the ViewController before this works.
I have also tried to access the image of the UITabBarItem from the initial ViewController, so that the changes go into effect as soon as the application is launched like so:

tabBarController.tabBar.items[2].image

But this throws an error and says that this API has been deprecated. Changing .image to .setImage doesn't say the API has been deprecated, but it throws an error nonetheless(unrecognized selector).

If you have any additional questions, please feel free to ask. Thanks in advance!
I am using Swift in XCode 6 Beta 6.

Upvotes: 3

Views: 7118

Answers (1)

Moon Cat
Moon Cat

Reputation: 2069

tabBar.items is an array of AnyObjects. Try casting the item to UITabBarItem

var myTabBarItem = tabBarController.tabBar.items[2] as UITabBarItem
myTabBarItem.image = UIImage(named: "PickleTabIcon").imageWithRenderingMode(UIImageRenderingMode.AlwaysOriginal)

Upvotes: 8

Related Questions