How to use SwiftUI in a UIKit project
• 1 min read
Did you know that SwiftUI can be use in UIKit project? To use SwiftUI in UIKit project, you have to make use of UIHostingViewController. It is a UIViewController that basically "host" a SwiftUI view.
Assuming you have a SwiftUI View named SwiftUIView like so:
let swiftUIView = SwiftUIView()Initializing it programmatically
To initialize it programmatically, set the SwiftUI View as the rootView of the UIHostingController.
let vc = UIHostingController(rootView: swiftUIView)
self.present(vc, animated: true, completion: nil)Initializing it in Storyboard
Using it in Storyboard requires a little bit more work.
First, add a Hosting View Controller and create a segue to it

Then, create an
IBSegueActionwhere you can init theUIHostingControllerlike so
@IBSegueAction func openSwiftUIView(_ coder: NSCoder) -> UIViewController? {
return UIHostingController(coder: coder, rootView: swiftUIView))
}That's it.