Swift为UITabBarItem设置徽章值

27

我正在尝试添加一个像附加的截图中的徽章警示标签。

enter image description here

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

欢迎提供任何建议!


你想引用带有白色“1”的红色徽章吗?“警示标签”并不是很具体 ;) - milo526
哈哈,我也是这么想的,但那是我能想到的最好的描述方式。是的,那个带有数字1的红色徽章就是我想要的。 - Gino
抱歉最近工作很忙,等我有时间了再去看一下并点赞。谢谢! - Gino
1
太棒了,谢谢,它可以运行... +1 - Gino
1个回答

67

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? - Milad.Nozari
7
找到答案了。因为我已经在一个UITabBarController中了,所以我必须从上面的代码中省略tabBarController。 - Milad.Nozari

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