Search code examples
swiftuitabbarcontrolleruitabbar

How to correctly display a tab controller in swift


I'm making a login screen for my app and everything works as intented until I try to present my main view after the login(which uses a Tab Bar Controller).

The only problem is that it displays just the first item on the tab bar. I have to press the other buttons for the to appear.

Im using this code:

//after login...
var storyboard: UIStoryboard = UIStoryboard(name: "Main", bundle: nil)

var vc: TabBarViewController = storyboard.instantiateViewControllerWithIdentifier("MainController") as! TabBarViewController

self.presentViewController(vc, animated: true, completion: nil)

My guess is that I need to load them all at the same time, but I dont know...


Solution

  • This is how I did it recently. I loaded my tabBarController and the login screen together, once the user has logged in (or completed the first screen experience) you can modally dismiss the controller.

    func showloginView() {
    
    let storyboard = UIStoryboard(name: "Main", bundle: nil)
    let loginViewController: LoginTableViewController = storyboard.instantiateViewControllerWithIdentifier("LoginTVC") as! LoginTableViewController
    self.window?.makeKeyAndVisible()
    
        var viewController = storyboard.instantiateViewControllerWithIdentifier("LoginTVC") as! LoginTableViewController
        let nav = UINavigationController(rootViewController: viewController)
        self.window?.rootViewController?.presentViewController(nav, animated: true, completion: nil)
    }
    

    for displaying your tabBarController and editing of any of the tabBarItems

    let tabBarController = self.window?.rootViewController as? UITabBarController
    let tabBarRootViewControllers: Array = tabBarController!.viewControllers!
    let nav = tabBarRootViewControllers[0] as? UINavigationController
    

    Hope this helps =)