How to present a view full-screen in SwiftUI?

早过忘川 提交于 2019-12-23 03:43:31

问题


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.

Thank you!


回答1:


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.

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.




回答2:


Encapsulate the body in a Group to eliminate compiler errors:

struct StartView: View {

@EnvironmentObject var userAuth: UserAuth

var body: some View {
    Group {
        if userAuth.isLoggedin {
            AppView()
        } else {
            LoginView()
        }

    }
}


来源:https://stackoverflow.com/questions/56557979/how-to-present-a-view-full-screen-in-swiftui

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!