如何在 SwiftUI 中全屏显示视图? [英] How to present a view full-screen in SwiftUI?

查看:50
本文介绍了如何在 SwiftUI 中全屏显示视图?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我曾经登录view,现在我想在登录后呈现view,但我不希望用户有可能返回登录<代码>查看.在 UIkit 中我使用了 present(),但它似乎在 SwiftUI presentation(_ modal: Modal?)view 不会占据整个屏幕.Navigation 也不是一个选项.

I worked to a login view, now I want to present the view after login, but I do not want the user to have the possibility to return to the login view. In UIkit I used present(), but it seems in SwiftUI presentation(_ modal: Modal?) the view does not take the entire screen. Navigation also isn't an option.

谢谢!

推荐答案

我不希望用户有可能返回到登录视图

I do not want the user to have the possibility to return to the login view

在这种情况下,您不应该离开登录视图,而是完全替换它.

In that case you shouldn't be presenting away from the login view but replacing it entirely.

您可以通过有条件地构建登录视图或应用程序视图"来实现这一点.

You could do this by conditionally building the login view or the "app view".

这样的东西...

// create the full screen login view
struct LoginView: View {
    // ...
}

//create the full screen app veiw
struct AppView: View {
    // ...
}

// create the view that swaps between them
struct StartView: View {
    @EnvironmentObject var isLoggedIn: Bool // you might not want to use this specifically.

    var body: some View {
        isLoggedIn ? AppView() : LoginView()
    }
}

通过使用这样的模式,您不会呈现或导航离开登录视图,而是完全替换它,因此它根本不再位于视图层次结构中.

By using a pattern like this you are not presenting or navigating away from the login view but you are replacing it entirely so it is no longer in the view hierarchy at all.

这可确保用户无法导航回登录屏幕.

This makes sure that the user cannot navigate back to the login screen.

同样...通过使用这样的 @EnvironmentObject,您可以稍后对其进行编辑(以退出),您的应用将自动返回到登录屏幕.

Equally... by using an @EnvironmentObject like this you can edit it later (to sign out) and your app will automatically be taken back to the login screen.

这篇关于如何在 SwiftUI 中全屏显示视图?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆