How to present a view full-screen in SwiftUI?

前端 未结 4 2046
长情又很酷
长情又很酷 2021-01-02 05:33

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

相关标签:
4条回答
  • 2021-01-02 06:08

    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.

    0 讨论(0)
  • 2021-01-02 06:12

    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()
            }
    
        }
    }
    
    0 讨论(0)
  • 2021-01-02 06:18
    struct ContentView: View {
        @EnvironmentObject var userAuth: UserAuth 
        var body: some View {
            if !userAuth.isLoggedin {
                return AnyView(LoginView())
            } else {
                return AnyView(HomeView())
            }
        }
    }
    
    0 讨论(0)
  • 2021-01-02 06:24

    I made this extension for myself. Any feedback/ideas are welcome. :)

    https://github.com/klemenkosir/SwiftUI-FullModal

    Usage

    struct ContentView: View {
    
        @State var isPresented: Bool = false
    
        var body: some View {
            NavigationView {
                Button(action: {
                    self.isPresented.toggle()
                }) {
                    Text("Present")
                }
                .navigationBarTitle("Some title")
            }
            .present($isPresented, view: ModalView(isPresented: $isPresented))
        }
    }
    
    struct ModalView: View {
    
        @Binding var isPresented: Bool
    
        var body: some View {
            Button(action: {
                self.isPresented.toggle()
            }) {
                Text("Dismiss")
            }
        }
    }
    
    0 讨论(0)
提交回复
热议问题