Posted on 09 Jul 2024 . 3 min read
With the introduction of SwiftUI, Apple has provided developers with a modern way to build user interfaces across all Apple platforms. However, many apps are still built using UIKit, and a complete rewrite might not be feasible. This is where UIHostingController comes into play, acting as a bridge that allows you to integrate SwiftUI views into your existing UIKit apps seamlessly. In this blog post, we’ll explore how to use UIHostingController to marry the old with the new.
UIHostingController is a class provided by Apple that allows you to embed SwiftUI views within a UIKit-based application. This enables developers to gradually adopt SwiftUI in their existing projects or add SwiftUI views to specific parts of their UIKit apps.
First, let’s create a simple SwiftUI view. This view will be embedded in our UIKit-based application.
import SwiftUI
struct ContentView: View {
var body: some View {
Text("Hello, SwiftUI!")
.padding()
}
}
Now, we’ll integrate this SwiftUI view into a UIKit view controller using UIHostingController.
import SwiftUI
import UIKit
class ViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
// Create a UIHostingController with the SwiftUI view
let contentView = ContentView()
let hostingController = UIHostingController(rootView: contentView)
// Add the hosting controller as a child view controller
addChild(hostingController)
view.addSubview(hostingController.view)
// Set up the constraints
hostingController.view.translatesAutoresizingMaskIntoConstraints = false
NSLayoutConstraint.activate([
hostingController.view.topAnchor.constraint(equalTo: view.topAnchor),
hostingController.view.bottomAnchor.constraint(equalTo: view.bottomAnchor),
hostingController.view.leadingAnchor.constraint(equalTo: view.leadingAnchor),
hostingController.view.trailingAnchor.constraint(equalTo: view.trailingAnchor)
])
// Notify the hosting controller that it has been moved to a parent
hostingController.didMove(toParent: self)
}
}
Another common scenario is presenting a SwiftUI view modally from a UIKit view controller.
let swiftUIView = ContentView()
let hostingController = UIHostingController(rootView: swiftUIView)
present(hostingController, animated: true, completion: nil)
You can also dynamically update the SwiftUI view displayed by the UIHostingController.
hostingController.rootView = AnotherSwiftUIView()
UIHostingController offers a powerful way to integrate SwiftUI views within UIKit applications, providing a flexible path for developers to adopt SwiftUI incrementally. Whether you are looking to modernize certain parts of your app or gradually transition to SwiftUI, UIHostingController is an invaluable tool. Start experimenting with it today and enjoy the best of both worlds!
By understanding and utilizing UIHostingController, you can effectively bridge the gap between UIKit and SwiftUI, leveraging the strengths of both frameworks to create compelling, modern applications.
Don’t hesitate to contact me if you have any questions or queries. Follow me on twitter @gurjitpt for any updates.
Thanks!
Written By

Gurjit Singh
I’m Computer Science graduate and CompTIA Security+ certified SOC Analyst and Mobile Application Security Engineer with 10+ years of cross-platform development experience across iOS, Android, and web.
Discover articles by topics
SwiftUI Class Struct Networking XCode NSCache Enum Optionals Property Observers Closures Guard Reviews StoreKit App Store Algorithms Testing Operators Protocol Extensions Weak Unowned SwiftData WWDC23 GCD API Admob SwiftLint Lottie Foreach Objective-C UIKit NavigationSplitView
Static analysis of an iOS app using MobSF, identifying credential misuse, security misconfigurations, and privacy issues....
2026-05-29 . 4 min read MobSF SAST
Transitioning from software development into cybersecurity is one of the most natural career moves in tech today....
2026-05-26 . 4 min read Security Certificates
Swift 6.1, officially released in March 2025, continues the evolution of Apple's powerful and expressive programming language....
2025-08-12 . 3 min read Swift 6.1
In any programming language, working with strings is essential, and Swift is no different.Whether you are building iOS apps......
2024-10-17 . 3 min read String Concatenation
With the introduction of SwiftUI, Apple has provided developers with a modern way to build user interfaces across all Apple platforms....
2024-07-09 . 3 min read UIHostingController
In the realm of software development, memory management plays a crucial role in ensuring the efficient allocation and deallocation of memory...
2024-01-28 . 4 min read Swift Autorelease