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

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

问题描述

我曾尝试登录view,现在我想在登录后显示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".

类似这样的事情...

Something like this...

// 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天全站免登陆