【问题标题】:Swift setting Badge Value for UITabBarItem为 UITabBarItem 快速设置徽章值
【发布时间】:2015-06-25 12:44:25
【问题描述】:

我正在尝试添加一个徽章警报标签,就像所附屏幕截图中的标签一样。

我尝试搜索标题、标签 uitabbar 项目,但卡住了。

欢迎提出任何建议。

【问题讨论】:

  • 您要参考红色徽章和白色“1”吗? “警报标签”并不是很具体;)
  • 哈哈,我也是这么想的,但这是我能想到的最好的描述方式。是的,带有 1 的红色徽章正是我想要的。
  • 抱歉最近工作很忙,有时间我会去看看并点赞。干杯
  • 太棒了,谢谢,它有效... +1

标签: swift swift2 uitabbarcontroller uitabbaritem


【解决方案1】:

Xcode 7.2.1 Swift 2.1.1

您只需为所需的 UITabBarItem 设置 badgeValue,如下所示:

tabBarController?.tabBar.items?[4].badgeValue = "1"   // this will add "1" badge to your fifth tab bar item


// or like this to apply it to your first tab
tabBarController?.tabBar.items?.first?.badgeValue = "1st"

// or to apply to your second tab
tabBarController?.tabBar.items?[1].badgeValue = "2nd"

// to apply it to your last tab
tabBarController?.tabBar.items?.last?.badgeValue = "Last"

要从 UITabBarItem 中删除徽章,只需为其添加 nil 值

tabBarController?.tabBar.items?.first?.badgeValue = nil

【讨论】:

  • 为什么 tabBarController 对我来说是 nil?
  • 找到了答案。因为我已经在一个 UITabBarController 里面,所以我不得不从上面的代码中省略 tabBarController。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2011-06-18
  • 2017-08-09
  • 2017-08-23
  • 2016-12-22
  • 2021-02-13
  • 1970-01-01
相关资源
最近更新 更多