top of page
Writer's pictureHui Wang

96. SwiftUI: How to activate/deactivate a Form

Here is an example of how you can activate and deactivate a Form in SwiftUI:

struct ContentView: View {
    @State private var disabledForm = false
    @State private var enableNotification = false
    @State private var userName = ""
    @State private var password = ""
    
    var body: some View {
        NavigationView {
            Form {
                Section(header: Text("Please enter your info:")) {
                    TextField("Username", text: $userName)
                    SecureField("Password", text: $password)
                    Toggle(isOn: $enableNotification) {
                        Text("Enable notifications")
                    }
                }
                .disabled(disabledForm)
            }
            .navigationBarTitle(Text("User"))
            .navigationBarItems(
                trailing:
                    Button(action: {
                        self.disabledForm.toggle()
                    }) {
                        Text(disabledForm ? "Activate Form" : "Deactivate Form")
                    }
            )
        }
    }
}


 

Follow me on:

Comments


bottom of page