Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SwiftUI onDismiss not firing when presenting a sheet (Xcode Beta 5)

Tags:

ios13

swiftui

In SwiftUI I am trying to get some code to run after a sheet has been dismissed but the 'onDismiss' closure in not called.

I'm running Xcode Beta 5, with an IOS 13 build target

import SwiftUI

struct ModalView : View {
    @Environment(\.presentationMode) var presentationMode: Binding<PresentationMode>
    var body: some View {
        Group {
            Text("Modal view")
            Button(action: { print("Dismiss button pressed"); self.presentationMode.value.dismiss() }) { Text("Dismiss") }
        }
    }
}

struct ContentView: View {
    @State var showModal: Bool = false
    var body: some View {
        Button(action: { self.showModal = true }) { Text("Show modal via .sheet modifier") }
        .sheet(isPresented: $showModal, onDismiss: { print("Modal Dismissed") }) { ModalView() }


    }
}

I should see:

Dismiss Button Pressed
Modal Dismissed

but I only get

Dismiss Button Pressed

Am I doing something stupid or is this another beta issue?

like image 483
lidders Avatar asked Aug 07 '19 21:08

lidders


1 Answers

Your code looks fine to me. onDismiss is called when you use the swipe gesture to dismiss the modal view (as you would expect), but it isn't called when you use self.presentationMode.value.dismiss() to dismiss the modal view, which just seems like a bug to me. I don't see any reason why your code shouldn't work.

A workaround in the meantime could be to pass an onDismiss closure to your ModalView, and then call it after calling self.presentationMode.value.dismiss().

like image 169
graycampbell Avatar answered Oct 15 '22 17:10

graycampbell