Search code examples
swiftstoryboarduitabbarcontroller

instantiateViewController with UITabBarController


I am having the following setup:

  • Login Screen -> after successfully logging in, a new view will be initiated
  • TabBarController -> attached to 4 different ViewControllers
  • MenuViewController -> this is the starting point of the 4 tabbars

I want to initiate the MenuViewController + the tabbar on bottom programmatically like this:

   let storyboard = UIStoryboard(name: "Employee", bundle: nil)
   let tabbarVC = storyboard.instantiateViewController(withIdentifier: "tabBar") as! UITabBarController
   self.present(tabbarVC, animated: false, completion: nil)

Unfortunately instead of initiating the view, simply nothing happens. Any ideas?


Solution

  • After @Bozzo Game's comment, I have been able to find the solution to this problem.

    The line self.present(tabbarVC, animated: false, completion: nil)

    has to be replaced with

    UIApplication.shared.keyWindow?.rootViewController = tabbarVC
    

    As the idea here was not to use present to display the tabbarVC on top of the current one, but to change the rootViewController of the Window.