如何为TabBarController使用自定义图像?

时间:2016-02-22 00:38:02

标签: ios swift

现在,我使用黑/白png作为我的图标,并在用户选择时使用tintColor。我觉得这太简单了。

我想使用多色图像(已选中),然后使用相同图像的灰度版本(未选中时)

如何为每个TabBar项目使用两个图像?

1 个答案:

答案 0 :(得分:1)

创建UITabBarController的子类,并将图标图片设置为UITableBarItem viewDidLoad()子类中的UITabBarController

override func viewDidLoad() {
    super.viewDidLoad()

    // Do any additional setup after loading the view.

    let tabBar: UITabBar = self.tabBar
    let tabBarItem1: UITabBarItem = tabBar.items![0]
    let tabBarItem2: UITabBarItem = tabBar.items![1]

    tabBarItem1.image = UIImage.init(named: "tab_icon_1_normal")?.imageWithRenderingMode(UIImageRenderingMode.AlwaysOriginal)
    tabBarItem2.image = UIImage.init(named: "tab_icon_2_normal")?.imageWithRenderingMode(UIImageRenderingMode.AlwaysOriginal)

    tabBarItem1.selectedImage = UIImage.init(named: "tab_icon_1_selected")?.imageWithRenderingMode(UIImageRenderingMode.AlwaysOriginal)
    tabBarItem2.selectedImage = UIImage.init(named: "tab_icon_2_selected")?.imageWithRenderingMode(UIImageRenderingMode.AlwaysOriginal)
}