I've done a bunch of searching on this question, and found numerous answers to accomplish this in Objective-C. However, I've yet to find an answer that is in Swift.
I've tried translating the Objective-C, with the following code executed in didFinishLaunchingWithOptions
:
if let rootViewController = self.window?.rootViewController {
print("root")
if let tabBarController = rootViewController.tabBarController {
print("tab")
let tabBarItem = tabBarController.tabBar.items![3]
tabBarItem.badgeValue = "!"
}
}
The code never prints "tab", so I'm obviously not accessing it correctly. Help?
Going on the assumption that your root view controller is actually the tab bar controller, you need to change:
if let tabBarController = rootViewController.tabBarController {
to:
if let tabBarController = rootViewController as? UITabBarController {