Integrate SwiftUI and UIKit: Two-Way Integration

Learn how to combine the power of SwiftUI and UIKit: A guide for both integrations: SwiftUI with UIKit, and UIKit with SwiftUI

ML Musings
3 min readJan 11, 2023
Photo by Thom Bradley on Unsplash

SwiftUI and UIKit are both frameworks to build user interfaces on Apple platforms, but they have distinct differences in terms of approach and syntax. SwiftUI is a newer framework that was introduced at WWDC 2019, and with surprisingly little code, it allows you to build user interfaces in a more efficient and intuitive way. UIKit, on the other hand, is an older framework that has been used for building iOS apps for many years, and it uses an imperative syntax.

Despite their differences, it is possible to integrate SwiftUI views into a UIKit based app, and vice versa. This allows developers to take advantage of the strengths of both frameworks and create more powerful and flexible apps.

Let us cut to the chase and look at the code for both ways integration.

Integrating SwiftUI into a UIKit App

To integrate a SwiftUI view into a UIKit based app, you can use the UIHostingControllerclass, which is a view controller that can display a SwiftUI view. You can use the code below to display a simple SwiftUI view in a UIKit based app:

--

--

ML Musings

✨ I enjoy pushing the boundaries of JS, Python, SwiftUI and AI. You can support my work through coffee - www.buymeacoffee.com/MLMusings