Dismiss sheet SwiftUI

Apple recommend (in WWDC 2020 Data Essentials in SwiftUI) using @State and @Binding for this. They also place the isEditorPresented boolean and the sheet's data in the same EditorConfig struct that is declared using @State so it can be mutated, as follows:

import SwiftUI

struct Item: Identifiable {
    let id = UUID()
    let title: String
}

struct EditorConfig {
    var isEditorPresented = false
    var title = ""
    var needsSave = false
    
    mutating func present() {
        isEditorPresented = true
        title = ""
        needsSave = false
    }
    
    mutating func dismiss(save: Bool = false) {
        isEditorPresented = false
        needsSave = save
    }
}

struct ContentView: View {
    
    @State var items = [Item]()
    @State private var editorConfig = EditorConfig()
    
    var body: some View {
        NavigationView {
            Form {
                ForEach(items) { item in
                    Text(item.title)
                }
            }
            .navigationTitle("Items")
            .toolbar {
                ToolbarItem(placement: .primaryAction) {
                    Button(action: presentEditor) {
                        Label("Add Item", systemImage: "plus")
                    }
                }
            }
            .sheet(isPresented: $editorConfig.isEditorPresented, onDismiss: {
                if(editorConfig.needsSave) {
                    items.append(Item(title: editorConfig.title))
                }
            }) {
                EditorView(editorConfig: $editorConfig)
            }
        }
    }
    
    func presentEditor() {
        editorConfig.present()
    }
}

struct EditorView: View {
    @Binding var editorConfig: EditorConfig
    var body: some View {
        NavigationView {
            Form {
                TextField("Title", text:$editorConfig.title)
            }
            .toolbar {
                ToolbarItem(placement: .confirmationAction) {
                    Button(action: save) {
                        Text("Save")
                    }
                    .disabled(editorConfig.title.count == 0)
                }
                ToolbarItem(placement: .cancellationAction) {
                    Button(action: dismiss) {
                        Text("Dismiss")
                    }
                }
            }
        }
    }
    
    func save() {
        editorConfig.dismiss(save: true)
    }
    
    func dismiss() {
        editorConfig.dismiss()
    }
}

struct ContentView_Previews: PreviewProvider {
    static var previews: some View {
        ContentView(items: [Item(title: "Banana"), Item(title: "Orange")])
    }
}

For me, beta 4 broke this method - using the Environment variable isPresented - of using a dismiss button. Here's what I do nowadays:

struct ContentView: View {
    @State var showingModal = false

    var body: some View {
        Button(action: {
           self.showingModal.toggle()
        }) {
           Text("Show Modal")
        }
        .sheet(
            isPresented: $showingModal,
            content: { ModalPopup(showingModal: self.$showingModal) }
        )
    }
}

And in your modal view:

struct ModalPopup : View {
    @Binding var showingModal:Bool

    var body: some View {
        Button(action: {
            self.showingModal = false
        }) {
            Text("Dismiss").frame(height: 60)
        }
    }
}

iOS 15+

Starting from iOS 15 we can use DismissAction that can be accessed as @Environment(\.dismiss).

There's no more need to use presentationMode.wrappedValue.dismiss().

struct SheetView: View {
    @Environment(\.dismiss) var dismiss

    var body: some View {
        NavigationView {
            Text("Sheet")
                .toolbar {
                    Button("Done") {
                        dismiss()
                    }
                }
        }
    }
}

Use presentationMode from the @Environment.

Beta 6

struct SomeView: View {
    @Environment(\.presentationMode) var presentationMode

    var body: some View {
        VStack {
            Text("Ohay!")
            Button("Close") {
                self.presentationMode.wrappedValue.dismiss()
            }
        }
    }
}

Tags:

Swiftui