Completely move to other view and don't allow to go back in SwiftUI

前端 未结 2 674
爱一瞬间的悲伤
爱一瞬间的悲伤 2021-01-13 03:36

I\'m developing a simple iOS app with SwiftUI with two views: a LogInView() and a HomeView().

What I want is really simple: when the user c

2条回答
  •  暗喜
    暗喜 (楼主)
    2021-01-13 04:03

    Update: I got some time to update the answer, and add a transition. Note that I changed Group by VStack, otherwise the transition does not work.

    You can alter the duration in the withAnimationcall (button closure).

    I also moved some modifiers in your button, so the whole thing is "tappable". Otherwise, only tapping on the text of the button would trigger the action.


    You can use an ObservedObject, an EnvironmentObject or a Binding. Here's an example with ObservedObject:

    import SwiftUI
    
    class Model: ObservableObject {
        @Published var loggedIn = false
    }
    
    struct ContentView: View {
        @ObservedObject var model = Model()
    
        var body: some View {
            VStack {
                if model.loggedIn {
                    HomeView().transition(.opacity)
                } else {
                    LogInView(model: model).transition(.opacity)
                }
            }
        }
    }
    
    struct HomeView: View {
        var body: some View {
            Text("Home Page")
        }
    }
    
    struct LogInView: View {
        @ObservedObject var model: Model
    
        var body: some View {
            VStack {
                Text("Welcome to Mamoot!")
                    .font(.largeTitle)
                    .fontWeight(.heavy)
                Text("We are glad to have you here.")
                Text("Please log in with your Mastodon or Twitter account to continue.")
                    .multilineTextAlignment(.center)
                    .lineLimit(4)
                    .padding()
                Spacer()
                //            FloatingTextField(title: "Username", placeholder: "Username", width: 300, type: "Username")
                //            FloatingTextField(title: "Password", placeholder: "Password", width: 300, type: "password")
                //                .padding(.top, -50)
                Spacer()
                ZStack {
                    Button(action: {
                        withAnimation(.easeInOut(duration: 1.0)) {
                            self.model.loggedIn = true
                        }
                    }) {
                        Text("Log in")
                            .foregroundColor(Color.white)
                            .bold()
                            .shadow(color: .red, radius: 10)
                            // moved modifiers here, so the whole button is tappable
                            .padding(.leading, 140)
                            .padding(.trailing, 140)
                            .padding(.top, 15)
                            .padding(.bottom, 15)
                            .background(Color.red)
                            .cornerRadius(10)
                    }
                }
                .padding(.bottom)
            }
        }
    }
    

提交回复
热议问题