How to Use UIHostingConfiguration to Integrate SwiftUI Views into UIKit Apps

UIHostingController is no longer the only choice.

Lee Kah Seng
4 min readApr 25, 2023

This article is originally published at swiftsenpai.com

Since the release of SwiftUI in 2019, Apple has been striving to enhance the interoperability between SwiftUI and UIKit. In iOS 16, Apple introduced UIHostingConfiguration, which is a powerful feature that enables developers to use SwiftUI views as UITableView or UICollectionView cells.

Recently, I came across an article that showed me another use case for UIHostingConfiguration that I was not aware of. It turns out that UIHostingConfiguration allows us to create a SwiftUI view that can be used in UIKit apps. I am extremely excited about this discovery and would like to share the details with you in this article.

How Does It Work

The concept behind is actually pretty straightforward. All we need to do is convert the SwiftUI view into a UIView. Let me show you how:

let config = UIHostingConfiguration {
Text("This is a SwiftUI view!")
}
let subview = config.makeContentView()
view.addSubview(subview)

If you take a look at the code above, you’ll notice that the makeContentView() method is where the magic happens. This…

--

--

Lee Kah Seng
Lee Kah Seng

Written by Lee Kah Seng

Support me by becoming a Medium member: https://leekahseng.medium.com/membership ⦿ 🇲🇾 Creator of https://swiftsenpai.com ⦿ iOS developer since 2011

No responses yet