无法在标签栏iOS Swift中显示图标

时间:2017-08-02 00:27:26

标签: ios swift uitabbarcontroller uitabbar

尽管在SO上发布了各种方法,但在初始化标签栏控制器时,我无法使用模拟器显示标签栏图标。

对于每个图标,我提供了3种尺寸,即25px乘25px,50px乘50px(@ 2x)和75px乘75px(@ 3x)。

这就是我的Tabbar在模拟器中的显示方式 enter image description here

以下分别是Tab和Image属性

enter image description here enter image description here

这是我75px x 75px(@ 3x)

的图标之一

enter image description here

有人可以指导我出错的地方吗?

3 个答案:

答案 0 :(得分:2)

适用于Swift 3.0

以编程方式设置所选标签和未选择的标签图像

    let arrayOfImageNameForSelectedState = ["tabBar_img_1", "tabBar_img_2", "tabBar_img_3"]
    let arrayOfImageNameForUnselectedState = ["tabBar_img_1", "tabBar_img_2", "tabBar_img_3"]

    if let count = self.tabBar.items?.count {

        for i in 0...(count-1) {

            let imageNameForSelectedState   = arrayOfImageNameForSelectedState[i]
            let imageNameForUnselectedState = arrayOfImageNameForUnselectedState[i]

            self.tabBar.items?[i].selectedImage = UIImage(named: imageNameForSelectedState)?.withRenderingMode(.alwaysOriginal)
            self.tabBar.items?[i].image = UIImage(named: imageNameForUnselectedState)?.withRenderingMode(.alwaysOriginal)
        }
    }

答案 1 :(得分:1)

我终于发现问题与xcassets文件夹有关。为了解决这个问题,我在Xcode中创建了一个新的xcassets文件夹。

  1. File > New > Resource > Asset Catalog

  2. 如下所示,我确保我的目标应用程序也已被选中。

  3. 我将旧xcassets文件夹中的图像拖放到新文件夹中,并且所有图片都已加载。

    enter image description here

答案 2 :(得分:0)

尝试修改如下: -

enter image description here

  

然后以编程方式更新自定义图像

if let tabBarItems = yourTabBar?.items {
  tabBarItems[0].selectedImage(UIImage(name: yourImage)
}