我想执行另一个视图控制器从注册,但当我这样做,边栏消失。有人帮忙吗?
@IBAction func signUpPressed(_ sender: UIButton) {
if let email = emailTextField.text, let parola = parolaTextField.text{
Auth.auth().createUser(withEmail: email, password: parola) { (authResult, eroare) in
if let e = eroare {
print(e.localizedDescription)
}
else
{
self.transitionToHome()
}
}
}
}
func transitionToHome() {
let homeViewController = storyboard?.instantiateViewController(identifier: Constants.Storyboard.homeViewController) as? HomeViewController
view.window?.rootViewController = homeViewController
view.window?.makeKeyAndVisible()
}
[ViewControllers][1]:https://i.stack.imgur.com/c6tsh.png
之所以会发生这种情况,是因为边栏按钮是导航控制器的一部分,并且当您尝试实例化homeViewController时,您只启动了controller,而该controller实际上没有NavigationVC。RootViewController应该是导航控制器。您需要从storyboard中的HomeViewController中删除标识符(constants.storyboard.HomeViewController),并将其设置为其嵌入的导航控制器。
func transitionToHome() {
let homeNavController = storyboard?.instantiateViewController(identifier: Constants.Storyboard.homeViewController) as? UINavigationController
view.window?.rootViewController = homeNavController
view.window?.makeKeyAndVisible()
}
transitionToHome()是自己创建一个视图控制器的实例,这就是左侧导航导航消失的原因。
如果您希望能够保持导航,那么您必须创建一个UINavigationController
,其中嵌入了HomeViewController
的根视图控制器
let homeNavigationController = UINavigationController(rootViewController: homeViewController)
view.window?.rootViewController = homeNavigationController
view.window?.makeKeyAndVisible()