更改 UITabBarItem 图像 [英] Changing UITabBarItem image
问题描述
我已经使用 Storyboard 设置了我的 UITabBarController
及其相应的 ViewControllers
.每当一个选项卡被取消选择时,它是灰色的,当它被选择时它有一个绿色调.我希望这些 UITabBarItems
中的一个看起来总是一样的:即,无论它是被选中还是被取消选中,它总是具有绿色色调.
此外,我用于此 UITabBarItem
的图标图像已经具有我想要的绿色外观.这很重要,因为我已经尝试在 UITabBarItem
的 ViewController
的 viewDidLoad
函数中使用此方法,我希望保持不变(我已经在 Storyboard 中的 UITabBarItem
和 ViewController
之间设置了一个 outlet
:
myTabBarItem.image =UIImage(named: "PickleTabIcon").imageWithRenderingMode(UIImageRenderingMode.AlwaysOriginal)
这很好用,但是它在 ViewController
加载之前不会被激活,所以用户在此工作之前,必须点击 TabItem
以加载 ViewController
.
我还尝试从初始 ViewController
访问 UITabBarItem
的图像,以便在应用程序启动后更改立即生效,如下所示:
tabBarController.tabBar.items[2].image
但这会引发错误并表示此 API 已被弃用.将 .image
更改为 .setImage
并不表示 API 已被弃用,但它仍然会引发错误(无法识别的选择器).
如果您有任何其他问题,请随时提出.提前致谢!
我在 XCode 6 Beta 6 中使用 Swift.
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.
推荐答案
tabBar.items
是 AnyObject
的数组.尝试将项目转换为 UITabBarItem
tabBar.items
is an array of AnyObject
s. Try casting the item to UITabBarItem
var myTabBarItem = tabBarController.tabBar.items[2] as UITabBarItem
myTabBarItem.image = UIImage(named: "PickleTabIcon").imageWithRenderingMode(UIImageRenderingMode.AlwaysOriginal)
这篇关于更改 UITabBarItem 图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!