在应用程序委托中设置 UITabBarItem 徽章值



我对这个问题进行了大量搜索,并在Objective-C中找到了许多答案来完成此操作。但是,我还没有找到 Swift 中的答案。

我尝试翻译Objective-C,并在didFinishLaunchingWithOptions中执行以下代码:

if let rootViewController = self.window?.rootViewController {
print("root")
if let tabBarController = rootViewController.tabBarController {
print("tab")
let tabBarItem = tabBarController.tabBar.items![3]
tabBarItem.badgeValue = "!"
}
}

代码从不打印"选项卡",所以我显然没有正确访问它。帮助?

假设您的根视图控制器实际上是选项卡栏控制器,您需要更改:

if let tabBarController = rootViewController.tabBarController {

自:

if let tabBarController = rootViewController as? UITabBarController {

最新更新