change tab bar item image in swift programatically

user6520705 picture user6520705 · Sep 7, 2016 · Viewed 11.3k times · Source

I am working on an app in Xcode. I currently have three tabBarItems on my tab bar. I want the middle one to be a picture chosen by the user. I have the desired picture in a variable and i have the tabbar.swift set up to put the code in. I just need to set the image to the variables image with correct size (and make the picture appear as a circle) and the title name to a string. Any help in doing this would be much appreciated. Thanks

Answer

Sofeda picture Sofeda · Sep 7, 2016

Try this code.

    self.tabBarController?.tabBar.items![0].image = UIImage(named: "your image name")
    // items![0] index of your tab bar item.items![0] means tabbar first item

    self.tabBarController?.tabBar.items![0].selectedImage = UIImage(named: "your image name")