Dismiss a SwiftUI View that is contained in a UIHostingController

I found another approach that seems to work well and which feels a little cleaner than some of the other approaches. Steps:

  1. Add a dismissAction property to the SwiftUI view:
struct SettingsUIView: View {
    var dismissAction: (() -> Void)
    ...
}    
  1. Call the dismissAction when you want to dismiss the view:
Button(action: dismissAction ) {
    Text("Done")
}
  1. 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 )

Leave a Comment

techhipbettruvabetnorabahisbahis forumueduseduedueduseduedusedusedusedusedus