简体   繁体   English

以编程方式更改标签栏项目图像

[英]change tab bar item image in swift programatically

I am working on an app in Xcode. 我正在使用Xcode中的应用程序。 I currently have three tabBarItems on my tab bar. 我目前在标签栏上有三个tabBarItems。 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. 我在变量中有所需的图片,我将tabbar.swift设置为放入代码。我只需要将图像设置为具有正确大小的变量图像(并使图片显示为圆圈)和标题命名为字符串。 Any help in doing this would be much appreciated. 任何帮助这样做将非常感激。 Thanks 谢谢

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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