I found another approach that seems to work well and which feels a little cleaner than some of the other approaches. Steps:
- Add a
dismissAction
property to the SwiftUI view:
struct SettingsUIView: View {
var dismissAction: (() -> Void)
...
}
- Call the
dismissAction
when you want to dismiss the view:
Button(action: dismissAction ) {
Text("Done")
}
- When you present the view, provide it with a dismissal handler:
let settingsView = SettingsUIView(dismissAction: {self.dismiss( animated: true, completion: nil )})
let settingsViewController = UIHostingController(rootView: settingsView )
present( settingsViewController, animated: true )