简体   繁体   中英

Unable to show icon in Tab Bar iOS Swift

Despite trying various methods posted on SO, I was unable to have the simulator display tab bar icons when initialising the tab bar controller.

For each icon, I provided 3 sizes ie 25px by 25px, 50px by 50px (@2x) and 75px by 75px (@3x).

This is how my Tabbar is showing up in the simulator 在此处输入图片说明

Below are the Tab and Image attributes respectively

在此处输入图片说明 在此处输入图片说明

Here is one of my icon at 75px x 75px (@3x)

在此处输入图片说明

Can someone please guide me where I am going wrong ?

For Swift 3.0

Set programmatically for selected Tab and un-selected Tab Images

    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)
        }
    }

I finally figured out that the problem was to do with the xcassets folder. To solve this I created a new xcassets folder in Xcode.

  1. File > New > Resource > Asset Catalog .

  2. As shown below I made sure my target application is selected as well.

I dragged and dropped the images from the old xcassets folder to the new one and all the images were loading.

在此处输入图片说明

Try modify like this:-

在此处输入图片说明

And then update your custom image programmatically

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM