2015-04-18 81 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。 –