change tab bar item image in swift programatically
Asked Answered
O

1

9

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

Okinawa answered 6/9, 2016 at 22:36 Comment(3)
Include your code, what you have tried, and what problems you are facing.Minter
self.tabBarController?.tabBar.items![0].image = UIImage(named: "your image name") self.tabBarController?.tabBar.items![0].selectedImage = UIImage(named: "your image name")Okinawa
and both failedOkinawa
R
14

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")
Rhoden answered 7/9, 2016 at 5:0 Comment(2)
I tried using this code in my tabbar.swift file but it didnt do anythingOkinawa
sorry.was in vacation. you are using custom tabbar.swift ? It will work where you want to change the image with the reference of tabBarController.Rhoden

© 2022 - 2024 — McMap. All rights reserved.