我对这个问题进行了大量搜索,并找到了许多在 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 = "!"
}
}
代码从不打印“制表符”,所以我显然没有正确访问它。帮忙?
最佳答案
继续假设您的 Root View Controller 实际上是标签栏 Controller ,您需要更改:
if let tabBarController = rootViewController.tabBarController {
到:
if let tabBarController = rootViewController as? UITabBarController {
关于swift - 在 appDelegate 中设置 UITabBarItem 徽章值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50341883/