简体   繁体   中英

NavigationBar doesn't appear

I trying to make an app with 2 view controllers. the first one is the LoginViewController and the second is the MainViewController the MainViewController has a navigation bar. I set the initial View controller in the SceneDelegate like this:

guard let windowScene = (scene as? UIWindowScene) else { return }
        window = UIWindow(frame: UIScreen.main.bounds)
        window?.windowScene = windowScene
        
 // a user is currently logged in

        if let _ = Auth.auth().currentUser {
          UIViewController.showViewController(storyBoardName: "Main", viewControllerId: "MainViewController")
        } else { 
            
 // no logged in user

          UIViewController.showViewController(storyBoardName: "Main", viewControllerId: "LoginViewController")
        }

        window?.makeKeyAndVisible()
      }
    }

This worked fine but the navigation bar doesn't appear, I searched and I found out that the initial View controller should be the navigation controller, not the MainViewController so I did this:

guard let windowScene = (scene as? UIWindowScene) else { return }
        window = UIWindow(frame: UIScreen.main.bounds)
        window?.windowScene = windowScene
        
 // a user is currently logged in

        if let _ = Auth.auth().currentUser {
          UIViewController.showViewController(storyBoardName: "Main", viewControllerId: "MainViewController")
        } else { 
            
 // no logged in user

  let vc = MainViewController()
            let nav = UINavigationController(rootViewController: vc)
            nav.navigationBar.barTintColor = .white
            nav.navigationBar.isTranslucent = false
            nav.navigationBar.tintColor = .black
            self.window = UIWindow(frame: UIScreen.main.bounds)
            self.window!.rootViewController = nav
            self.window?.makeKeyAndVisible()
}
    }

but still, the navigationbar doesn't show

You will have to make a MainViewController as a root after the user is logged in

    func scene(_ scene: UIScene, willConnectTo session: UISceneSession, options connectionOptions: UIScene.ConnectionOptions) {
    // Use this method to optionally configure and attach the UIWindow `window` to the provided UIWindowScene `scene`.
    // If using a storyboard, the `window` property will automatically be initialized and attached to the scene.
    // This delegate does not imply the connecting scene or session are new (see `application:configurationForConnectingSceneSession` instead).
    
    guard let _ = (scene as? UIWindowScene) else { return }
    
    // a user is currently logged in
    
    if isUserLoggedIn {
        
        let vc = LoginViewController()
        let nav = UINavigationController(rootViewController: vc)
        nav.navigationBar.barTintColor = .white
        nav.navigationBar.isTranslucent = false
        nav.navigationBar.tintColor = .black
        self.window = UIWindow(frame: UIScreen.main.bounds)
        self.window!.rootViewController = nav
        self.window?.makeKeyAndVisible()
        
    } else {
        let vc = ViewController()
        let nav = UINavigationController(rootViewController: vc)
        nav.navigationBar.barTintColor = .white
        nav.navigationBar.isTranslucent = false
        nav.navigationBar.tintColor = .black
        self.window = UIWindow(frame: UIScreen.main.bounds)
        self.window!.rootViewController = nav
        self.window?.makeKeyAndVisible()
        
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM