I am trying to call a TabBarController in Swift 5 but I am having some problems with the way it is displayed. I hope someone can help me. With this code I manage to call the TabController, but the way it is presented is not what I am looking for.
Code:
let mainTabController=storyboard?.instantiateViewController(identifier:
"MainTabBarController") as! UITabBarController
present(mainTabController, animated: true, completion: nil)
It doesn't work like this:
self.navigationController?.pushViewController(mainTabController, animated: true) - (de este modo ni siquiera abre el TabController)
The hierarchy you have used to handle the navigation of your views is not correct. I do not recommend that you instantiate the view of the TabBarController by code, below I explain how you can do it from the Storyboard.
I take this opportunity to explain to you why when you try to push the TabBarController in this way it doesn't work.
Basically, it's because it
self.navigationController?
's nil. A NavigationController does not exist because the ViewController is not attached to or does not belong to a navigation.You always have to connect your initial view with a
UINavigationController
, with this, all the views that you present from root will be connected, thus creating a navigation stack. Now, if you are also going to use aUITabBarController
solo, you should push it and that's it.This image explains it quite well.