Swift设置UITabBarItem的徽章值

Gin*_*ino 26 uitabbarcontroller uitabbaritem swift swift2

我正在尝试添加徽章警报标签,如附带的屏幕截图中的那个.

在此输入图像描述

我试图搜索标题,标签uitabbar项目,但我卡住了.

任何建议表示赞赏.

Leo*_*bus 66

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"
Run Code Online (Sandbox Code Playgroud)

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

tabBarController?.tabBar.items?.first?.badgeValue = nil
Run Code Online (Sandbox Code Playgroud)

  • 找到了答案.因为我已经在UITabBarController中,所以我不得不从上面的代码中省略tabBarController. (6认同)