我试着做一个有两个视图控制器的应用程序。第一个是LoginViewController,第二个是MainViewController,MainViewController有一个导航栏。我在SceneDelegate中设置了初始视图控制器,如下所示:
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()
}
}这很好用,但是导航栏没有出现,我搜索了一下,发现初始的视图控制器应该是导航控制器,而不是MainViewController,所以我这样做:
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()
}
}但是,导航栏仍然没有显示
发布于 2021-03-04 22:15:59
在用户登录后,您必须以根用户身份创建MainViewController
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()
}
}https://stackoverflow.com/questions/66469656
复制相似问题