【问题标题】:change tab bar item image in swift programatically以编程方式快速更改标签栏项目图像
【发布时间】:2017-01-14 11:43:16
【问题描述】:

我正在 Xcode 中开发一个应用程序。我的标签栏上目前有三个 tabBarItems。我希望中间一张是用户选择的图片。我在变量中有所需的图片,并且设置了 tabbar.swift 以放入代码。我只需要将图像设置为具有正确大小的变量图像(并使图片显示为圆形)和标题名称为字符串。 对此的任何帮助将不胜感激。谢谢

【问题讨论】:

  • 包括您的代码、您尝试过的内容以及遇到的问题。
  • self.tabBarController?.tabBar.items![0].image = UIImage(named: "你的图片名称") self.tabBarController?.tabBar.items![0].selectedImage = UIImage(命名:“您的图像名称”)
  • 都失败了

标签: ios swift tabbar tabbarcontroller


【解决方案1】:

试试这个代码。

    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 文件中使用此代码,但它没有做任何事情
  • 抱歉。当时正在度假。您正在使用自定义 tabbar.swift 吗?它将在您想要使用 tabBarController 的引用更改图像的地方工作。
猜你喜欢
  • 1970-01-01
  • 2015-02-13
  • 2023-03-30
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-02-19
  • 1970-01-01
相关资源
最近更新 更多