Search code examples
iosuinavigationcontrolleruinavigationbaruistoryboard

NavigationTab for subsequent Views not working


I have a LoginView, where after user authentication the user is sent to a corresponding homeView. The transition is made programmatically without segue. Then, depending on the user type (lets say, superuser and tester), the user can browse from his homeView. Both homeViews are preceded by a NavigationController.


                 NavigationController -Segue->  SuperView  -Segue-> SecondView 
->LoginView                                                              |
                 NavigationController -Segue->      HomeView    <-Segue -
                                                       |
                                                        -Segue->  View4 -Segue-> View5 ...

Here is the storyboard flow

And the problem: in the simulator, after the login, the first view looks great, but all the next views don't have a navigation bar. In the storyboard, however, everything looks fine.

The transition is made as follows:

func transitionToSuperHome() {

        let supervisorViewController = storyboard?.instantiateViewController(identifier: "superVC") as? SuperuserViewController

        view.window?.rootViewController = superuserViewController
        view.window?.makeKeyAndVisible()
    }

and

func transitionToHome() {

       let homeViewController = storyboard?.instantiateViewController(identifier: "homeVC") as? HomeViewController
        view.window?.rootViewController = homeViewController
        view.window?.makeKeyAndVisible()
    }

This is how the second (and all subsequent) view looks like

any idea what can cause the problem?


Solution

  • It looks to me like you are setting the root view controller in a detached state. A simple fix might be to embed your root view controller inside of a navigation controller in order to avoid the detached state. This is also why your screenshots appear to be presented modally instead of in a navigation stack.

    window?.rootViewController = UINavigationController(rootViewController: homeViewController)