在Swift中以编程方式更改选项卡栏项目图像

9

我正在使用Xcode开发一个应用程序。 我的选项卡栏上目前有三个选项卡。 我希望中间那个可以由用户选择一个图片。 我已经将所需的图片存储在变量中,并已设置好tabbar.swift来放置代码。 我只需要将图片设置为变量的图像,大小正确(并将图片呈现为圆形),并将标题名称设置为一个字符串。 任何帮助都会非常感激。 谢谢


请在您的问题描述中包含代码、尝试过的方法以及遇到的问题。 - Michael
self.tabBarController?.tabBar.items![0].image = UIImage(named: "你的图片名称") self.tabBarController?.tabBar.items![0].selectedImage = UIImage(named: "你的图片名称") - user6520705
两者都失败了。 - user6520705
1个回答

14

试试这段代码。

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

我尝试在我的tabbar.swift文件中使用这段代码,但它没有任何作用。 - user6520705
2
抱歉,我在度假。你正在使用自定义的tabbar.swift吗?如果你想要通过tabBarController的引用来更改图像,它会起作用。 - Sofeda

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接