0

I am trying to change tab bar in didFinishLaunchingWithOptions method programmatically, but it won't work, any idea?

I have tried:

    func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {

    if let tabBarController = self.window!.rootViewController as? UITabBarController {
                tabBarController.selectedIndex = 0
            }
return true
}

Thanks,

Steven
  • 762
  • 1
  • 10
  • 27

1 Answers1

0

are you sure that your root view controller is UITabBarController? because it would seem that you do not enter the if-let. It's possibile that you've a navigation controller that contains your tab viewcontroller?

Alessio Campanelli
  • 970
  • 11
  • 19
  • I have a class which HomeTabBarController which inherit from UITabBarController, I tried with HomeTabBarController instead of UITabBarController, but it didn't work – Steven Oct 04 '19 at 13:28
  • YES, it doesn't enter. – Steven Oct 04 '19 at 13:30
  • check from debug if 'self.window!.rootViewController' is an instance of HomeTabBarController – Alessio Campanelli Oct 04 '19 at 13:30
  • ok, you use a navigation controller ? it's possibile that you must check a child of navigation controller (that in your case it would be root viewcontroller) – Alessio Campanelli Oct 04 '19 at 13:31
  • in this case you could use this check https://stackoverflow.com/questions/42826026/how-to-check-viewcontroller-is-added-in-stack-or-not – Alessio Campanelli Oct 04 '19 at 13:37
  • Thanks @Alessio, l don't use in my appdelegate navigation controller, so in this case if let viewControllers = self.navigationController?.viewControllers won't work for me, I tried to print if let viewControllers = window?.rootViewController?.children but some how it shows nil.. – Steven Oct 04 '19 at 13:49
  • ok, but in debug your rootViewController to which class does it belong? – Alessio Campanelli Oct 04 '19 at 13:56
  • Thanks @Alessio, I didn't recognize that my root controller was assign to another view controller, I resolve the issue. thanks – Steven Oct 04 '19 at 14:22