在Swift中更改UITabBarItem上的selectedImage [英] Changing selectedImage on UITabBarItem in Swift
问题描述
我一直在尝试更改UITabBar上的所选图像。我已经按照其他Stackoverflow问题列出的程序,但似乎没有任何工作。
I've been trying to change the selected image on a UITabBar. I've followed procedures listed on other Stackoverflow questions, but nothing seems to work.
我尝试通过用户定义的运行时属性部分设置图像,以及尝试过将以下内容添加到AppDelegate.swift:
I have tried setting the image through the User Defined Runtime Attributes section, as well as tried adding the following to AppDelegate.swift:
var tabBarController = self.window!.rootViewController as UITabBarController
let tabItems = tabBarController.tabBar.items as [UITabBarItem]
var selectedImage0 = UIImage(named:"NewsfeedTabSelected")
selectedImage0?.imageWithRenderingMode(UIImageRenderingMode.AlwaysOriginal)
tabItems[0].selectedImage = selectedImage0
这不会产生任何结果。我做了一个 println(tabItems [0] .title)并输出了正确的标题,所以我知道对TabBarItem的引用是有效的。
This doesn't yield any results. I did a println(tabItems[0].title) and that output the correct title, so I know that the reference to the TabBarItem is working.
有什么想法?
推荐答案
我在AppDelegate上使用类似的东西解决了。
I solved using something like this on the AppDelegate.
var tabBarController = self.window!.rootViewController as UITabBarController
var tabBar = tabBarController.tabBar as UITabBar
var tabBarItem1 = tabBar.items![0] as UITabBarItem
var tabBarItem2 = tabBar.items![1] as UITabBarItem
var tabBarItem3 = tabBar.items![2] as UITabBarItem
tabBarItem1.selectedImage = UIImage(named: "FirstSelectedImage")
tabBarItem2.selectedImage = UIImage(named: "SecondSelectedImage")
tabBarItem3.selectedImage = UIImage(named: "ThirdSelectedImage")
这篇关于在Swift中更改UITabBarItem上的selectedImage的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!