Dismiss presented sheet from other/root views in SwiftUI

Viewed 26

I want to dismiss any presented sheet on receiving deeplink or notification. This was possible in UIKit by checking rootview/navigation controller has presented modal and then easily you can dismiss it. How can I achieve the same behavior by using SwiftUI?

1 Answers

One way is like this:

.sheet(isPresented: $sheetConfig.isPresented) {

}
.onOpenURL { url in 
    sheetConfig.hide()
}

struct SheetConfig {
    var isPresented = false

    // other vars for sheet data

    mutating func hide() {
        isPresented = false
    }
}
Related