Search code examples
iosswiftxcodeviewcontroller

How to Segue from a NavigationController to a TabBarController without taking the Navbar with it in Swift 5?


I am currently working on a small iOS Project and encountered a problem:

The App starts with a Navigation View Controllerfor login and registration purpose. When the registration is finished on the last view controller in this NavigationVC, I would like to redirect to the main Tab Bar Controller.

See this image. I hope it can illustrate what I meant above: enter image description here

The problem is the missing segue between the left and the right side. All the proposed Segues from Xcode doesn't work. The modal Segue is no option and the show Segue takes the NavBar of the NavigationController with it.

I hope you guys have ideas on how to solve this problem. Thanks for your help in advance. Have a great day!


Solution

  • You may reconsider your navigation architecture.

    Let's name the first view controller of your tabBarController "HomeViewController"

    One of the way to implement it is:

    • always load your tabBarController at launch.
    • check if the user is logged in inside the viewDidLoad method of the HomeViewController.
    • if the user in not logged in, present the login view controller (with his own navigation controller).

    You have now presented the login process on top of the HomeViewController.

    When the registration is done, dismiss the loginViewController.

    You are now back to the HomeViewController and the registration is done.

    Additionally, you can use NotificationCenter to warn the HomeViewController that a user just logged in and perform any initial work.