2015-04-18 85 views
23

我正在尝试添加徽标警报标签,如附件中的截图所示。UITabBarItem的快速设置徽章值

enter image description here

我试着搜索标题,标签uitabbar项目,但我坚持。

任何建议表示赞赏。

+0

你想参考红色的徽章与白色'1'? “一个警示标签”并不是特定的;) – milo526

+0

哈哈,我也这么认为,但这是我能想到的最好的方式来描述它。是的,那个1的红色徽章就是我想要的。 – Gino

+0

对不起,最近工作非常忙碌,我会看看,一旦我有时间,并upvote它。干杯 – Gino

回答

59

的Xcode 7.2.1斯威夫特2.1.1

你只需要设置badgeValue您所需的UITabBarItem如下:

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只需添加零值它

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

为什么我的tabBarController是零? –

+5

找到了答案。因为我已经在UITabBarController中,所以我不得不从上面的代码中省略tabBarController。 –