Hekes Pekes
Hekes Pekes

Reputation: 1325

Display SwiftUI modal from UIKit view

I have got a modal sheet built with SwiftUI but the rest of my app is UIKit. I want to display modal when user taps a button in UIKit. How do I implement this?

Upvotes: 4

Views: 3114

Answers (2)

Hekes Pekes
Hekes Pekes

Reputation: 1325

Create a UIHostingController with your SwiftUI view set as its rootView, and present that as normal.

let hostingController = UIHostingController(rootView: MyModalView())
self.present(hostingController, animated: true, completion: nil)

Upvotes: 8

Mac3n
Mac3n

Reputation: 4719

You can load your SwiftUI view using UIHostingController(rootView: YourSwiftUIView()) and present it in your UIKit

Upvotes: 3

Related Questions