TabBarItem徽章值未显示

时间:2015-11-07 10:21:18

标签: ios swift uitabbaritem

我写了这段代码,但似乎没有用,我不知道为什么,TabBarItem是正确的,但徽章没有显示。

let mainStoryboard: UIStoryboard = UIStoryboard(name: "Main", bundle: nil)
let tabBarController = mainStoryboard.instantiateViewControllerWithIdentifier("TabBar") as! UITabBarController
let tabArray = tabBarController.tabBar.items as NSArray!
var chatItem = tabArray.objectAtIndex(3) as! UITabBarItem
chatItem.badgeValue = "1"

1 个答案:

答案 0 :(得分:0)

更新值时必须重新加载tabBarController,所以请执行:

let tabBarController = self.storyboard?.instantiateViewControllerWithIdentifier("TabBar") as! TabsViewController // Your tabor controller
tabBarController.selectedIndex = 2 // Whatever your selected index

let tabArray = tabBarController.tabBar.items as NSArray!
let chatItem = tabArray.objectAtIndex(3) as! UITabBarItem
chatItem.badgeValue = "1"
var appDelegate = UIApplication.sharedApplication().delegate as! AppDelegate
appDelegate.window?.rootViewController = tabBarController