简体   繁体   English

如何使用 SwiftUI 更改根视图

[英]How do I change the root view with SwiftUI

I want to replace a view with another view using SwiftUI.我想使用 SwiftUI 将一个视图替换为另一个视图。

Currently I use either a NavigationView or.sheet to transition to a new view and that works fine stacking views.目前我使用 NavigationView 或 .sheet 来转换到新视图,并且可以很好地堆叠视图。

However I have not found a way to completely replace a view with another view.但是我还没有找到用另一个视图完全替换视图的方法。 For example when the user first lands on the App it will give an option to sign in or create a new account.例如,当用户第一次登陆应用程序时,它将提供登录或创建新帐户的选项。 I want either option to replace the previous view, not present on top of it.我想要任何一个选项来替换以前的视图,而不是出现在它上面。 Another example is when the user logs out of the app.另一个例子是当用户退出应用程序时。 I want to dismiss my entire stack and replace with login screen.我想关闭我的整个堆栈并替换为登录屏幕。 Not just dismiss all overlying views.不只是忽略所有重叠的观点。

The analogy to pre swiftUI would be to replace the rootView.与前 swiftUI 的类比是替换 rootView。

You just display each view conditionally in a root view您只需在根视图中有条件地显示每个视图

var body: some View {
   if isLoggedIn {
      HomeView()
   } else {
      LoginView() {
   }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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