文字っぽいの。

文字を書いています。写真も混ざります。

SwiftUIで表示したModalをコードからdismissしたい

やりたいこと

f:id:FromAtom:20201107232000g:plain

いわゆる dismiss(animated:completion:) をしたい。

コード

struct ContentView: View {
    @State private var showModal = false

    var body: some View {
        Button("モーダルを開く") {
            showModal.toggle()
        }.sheet(isPresented: $showModal) {
            ModalView()
        }
    }
}

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

    var body: some View {
        Button("閉じる") {
            presentationMode.wrappedValue.dismiss()
        }
    }
}