I'm trying to add a viewController to the existing Tab Bar programmatically through didFinishLaunchWithOptions
, but my code doesn't work, i can't downcast the rootViewController as UITabBarController.
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey : Any]? = nil) -> Bool {
if let tabBarController = window?.rootViewController as? UITabBarController {
print("rootVC")
let storyboard = UIStoryboard(name: "Main", bundle: nil)
let vc = storyboard.instantiateViewController(withIdentifier: "NavController")
vc.tabBarItem = UITabBarItem(tabBarSystemItem: .topRated, tag: 1)
tabBarController.viewControllers?.append(vc)
}
return true
}
Here is my storyboard, i think here is all okay, Tab Bar Contoller is initial view Controller...
Main.storyboard