When building an application that supports data entry, you'll almost always need to include a modal form of some kind. In this lesson, we'll look at presenting a modal using the fullScreenCover modifier. In addition to this, I will also show various ways of dismissing these types of modals with @Binding vs Environment(.presentationMode).
struct MainView: View { // 1 @State private var shouldPresentAddCardForm = false var body: some View { NavigationView { ScrollView { // .... // 2 Spacer() .fullScreenCover(isPresented: $shouldPresentAddCardForm, onDismiss: nil) { AddCardForm() } } // ... } }
struct AddCardForm: View { // 1 @Environment(\.presentationMode) var presentationMode @State private var name = "" var body: some View { NavigationView { Form { Text("Add card form") TextField("Name", text: $name) } .navigationTitle("Add Credit Card") .navigationBarItems(leading: Button(action: { // 2 presentationMode.wrappedValue.dismiss() }, label: { Text("Cancel") })) } }}