Navigation Bar disappearing when using RootViewController

I am trying to see if the user is logged in using the userdefaults, and if he is, to display the main view controller. This is the scene delegate code.

var window: UIWindow?
func scene(_ scene: UIScene, willConnectTo session: UISceneSession, options connectionOptions: UIScene.ConnectionOptions) {
        
        if UserDefaults.standard.bool(forKey: "isLoggedIn") && false {
            
            let homeViewController = UIStoryboard(name: "Main", bundle: nil).instantiateViewController(withIdentifier: "MainStoryBoard") as! MainViewController
            let navigationController = UINavigationController(rootViewController: homeViewController)
            window?.rootViewController = navigationController
            
            
        } else {
            let homeViewController = UIStoryboard(name: "Main", bundle: nil).instantiateViewController(withIdentifier: "InitialViewController") as! InitialViewController
            let navigationController = UINavigationController(rootViewController: homeViewController)
            window?.rootViewController = navigationController
        }
        
        
        window?.makeKeyAndVisible()
    }

This is what it's supposed to look like:

However, this is what it looks like :

We can see the navigation bar is totally gone.

This is how my views look like. I have a tab view controller linked to a navigation view controller linked to the main view controller I would like to present.

Thanks!

Answered by Marco_Boi123 in 750616022

This was the code that worked for me

            
            guard let tabBarController = storyboard.instantiateViewController(withIdentifier: "TabBarController") as? UITabBarController else {
                fatalError("Unable to instantiate tab bar controller")
            }
            tabBarController.modalPresentationStyle = .fullScreen
            
            guard let windowScene = (scene as? UIWindowScene) else {
                fatalError("Unable to get window scene")
            }
            let window = UIWindow(windowScene: windowScene)
            window.rootViewController = tabBarController
            self.window = window
            window.makeKeyAndVisible()
if UserDefaults.standard.bool(forKey: "isLoggedIn") && false {

AND FALSE?

false == false; it'll never be true, so even if the user is logged in, you're trying to evaluate "(the user his logged in) and (false == true)".

Accepted Answer

This was the code that worked for me

            
            guard let tabBarController = storyboard.instantiateViewController(withIdentifier: "TabBarController") as? UITabBarController else {
                fatalError("Unable to instantiate tab bar controller")
            }
            tabBarController.modalPresentationStyle = .fullScreen
            
            guard let windowScene = (scene as? UIWindowScene) else {
                fatalError("Unable to get window scene")
            }
            let window = UIWindow(windowScene: windowScene)
            window.rootViewController = tabBarController
            self.window = window
            window.makeKeyAndVisible()
Navigation Bar disappearing when using RootViewController
 
 
Q