Changing UITabBarItem image

…衆ロ難τιáo~ 提交于 2019-12-23 17:11:33

问题


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.


回答1:


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)


来源:https://stackoverflow.com/questions/25490337/changing-uitabbaritem-image

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!