jhaywoo8
jhaywoo8

Reputation: 767

Tab Bar Item Image not showing up

I have an image in the assets folder and it isn't showing up on the tab bar. I've set the "Render as" to "Original Image" as other answers have said to do but that doesn't fix the issue.

class CustomTabBarController: UITabBarController {
override func viewDidLoad() {
    super.viewDidLoad()


    let homeController = HomeController(collectionViewLayout: UICollectionViewFlowLayout())
    let navigationController = UINavigationController(rootViewController: homeController)
    navigationController.tabBarItem.image = UIImage(named:"news_feed_icon")

    viewControllers = [homeController]

    }
}

Upvotes: 2

Views: 2188

Answers (3)

mfaani
mfaani

Reputation: 36287

I'm not sure why, but you need to follow a different sequence of the same code. That is first add your viewController(s) to the tabBar then add its image.

class CustomTabBarController: UITabBarController {
override func viewDidLoad() {
    super.viewDidLoad()


    let homeController = HomeController(collectionViewLayout: UICollectionViewFlowLayout())
    let navigationController = UINavigationController(rootViewController: homeController)

    //First do this 
    viewControllers = [homeController]

    //Then add image
    navigationController.tabBarItem.image = UIImage(named:"news_feed_icon")  
    }
}

Upvotes: 0

Brandon A
Brandon A

Reputation: 8279

There could be a couple of reasons why this is happening:

1) You may have entered the wrong name for the image

2) The image may be of a size that is too large to show up. Tab Bar item images should be around 28pt-32pt (pixels) in size.

3) It also may not be the correct format. Tab Bar images should be PNG that are rendered with one solid color. If you are trying to load a JPEG of a tree in a park or something, might not work out so well.

Also,

I have found you need to explicitly create a new Tab Bar item if you wish to change the image of a UITabBarItem. I typically remove all items from my tab bar and build all new items with the new image if I ever need to "update" and image to a UITabBarItem.

So if that is your issue I would suggest writing a function in your view controller that will do just that and in your viewDidLoad above, just call that function on your controller.

Upvotes: 1

KKRocks
KKRocks

Reputation: 8322

Try this .

Add in viewDidLoad

let customTabBarItem:UITabBarItem = UITabBarItem(title: nil, image: UIImage(named: "news_feed_icon")?.withRenderingMode(UIImageRenderingMode.alwaysOriginal), selectedImage: UIImage(named: "news_feed_icon"))
    self.tabBarItem = customTabBarItem

Upvotes: 3

Related Questions