UIHostingController: SwiftUI Inside UIKit
Embedding SwiftUI views into UIKit apps with UIHostingController.
UIHostingController: SwiftUI Inside UIKit is a free Swift Academy lesson on CoddyKit. This is lesson 4 of 4. Any 3 lessons of this learning path are free to read in full — after that, CoddyKit PRO unlocks every lesson, plus hands-on practice with a built-in code editor and a 24/7 AI tutor. It is part of the Swift Academy learning path, and your progress syncs across the web and the CoddyKit app. The Swift Academy course includes 4 lessons in total.
What is UIHostingController?
UIHostingController is a UIKit view controller that hosts a SwiftUI view hierarchy.
import SwiftUI
let swiftUIView = Text("Hello from SwiftUI")
let hostingVC = UIHostingController(rootView: swiftUIView)Embedding in a UIViewController
Add a hosting controller as a child view controller using the standard child VC API.
class MyViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
let child = UIHostingController(rootView: ContentView())
addChild(child)
view.addSubview(child.view)
child.view.frame = view.bounds
child.didMove(toParent: self)
}
}Auto Layout with Hosting Controller
Pin the hosting controller's view with Auto Layout to integrate it into a UIKit layout.
child.view.translatesAutoresizingMaskIntoConstraints = false
NSLayoutConstraint.activate([
child.view.topAnchor.constraint(equalTo: view.safeAreaLayoutGuide.topAnchor),
child.view.leadingAnchor.constraint(equalTo: view.leadingAnchor),
child.view.trailingAnchor.constraint(equalTo: view.trailingAnchor),
child.view.bottomAnchor.constraint(equalTo: view.bottomAnchor)
])Updating the Root View
Update the hosted SwiftUI view by setting rootView on the hosting controller.
let hostingVC = UIHostingController(rootView: MyView(count: 0))
// Later:
hostingVC.rootView = MyView(count: 5)Presenting Modally
Present a hosting controller like any UIKit modal, gaining SwiftUI content in a UIKit presentation.
let vc = UIHostingController(rootView: MySwiftUIView())
present(vc, animated: true)Push to Navigation Stack
Push a hosting controller onto a UINavigationController to navigate to SwiftUI content from UIKit.
let vc = UIHostingController(rootView: DetailView())
navigationController?.pushViewController(vc, animated: true)Sharing Data via ObservableObject
Pass a shared ObservableObject into the SwiftUI view's environment for two-way data flow.
let model = SharedModel()
let vc = UIHostingController(rootView: SwiftUIView().environmentObject(model))
// Changes to model in UIKit update SwiftUI and vice versaSizing the Hosting Controller
Use sizeThatFits to compute the intrinsic size of the SwiftUI content for embedding in scroll views.
let size = hostingVC.sizeThatFits(in: CGSize(width: 320, height: UIView.layoutFittingCompressedSize.height))
hostingVC.view.frame.size = sizeAppearance and Style
Set overrideUserInterfaceStyle on the hosting controller to control dark/light mode independently.
hostingVC.overrideUserInterfaceStyle = .darkScene-Based App Setup
In a UIKit-first app, you can replace the root view controller with a hosting controller to migrate incrementally.
func scene(_ scene: UIScene, willConnectTo session: UISceneSession, options: UIScene.ConnectionOptions) {
guard let windowScene = scene as? UIWindowScene else { return }
let window = UIWindow(windowScene: windowScene)
window.rootViewController = UIHostingController(rootView: ContentView())
self.window = window
window.makeKeyAndVisible()
}Use Cases
UIHostingController is ideal for incremental migration: keep UIKit navigation while embedding new SwiftUI feature screens.
// UIKit app gradually migrated:
// - Old screens: UIViewController
// - New screens: UIHostingController(rootView: NewFeatureView())Quick Check
What UIKit class do you use to embed a SwiftUI view inside a UIKit view controller hierarchy?
Lesson Recap
UIHostingController(rootView:) bridges SwiftUI into UIKit. Add it as a child view controller with Auto Layout constraints, or present/push it. Update via rootView. Share data via ObservableObject and environmentObject.
Learn Swift with an AI tutor — free
Write and run real code in your browser, get instant help from a 24/7 AI tutor, and pick up where you left off on the web or in the app.
- Courses
- 122
- Lessons
- 409
Frequently Asked Questions
Is the “UIHostingController: SwiftUI Inside UIKit” lesson free?
Yes — any 3 lessons of the Swift Academy learning path, including “UIHostingController: SwiftUI Inside UIKit”, are free to read in full here on the web. After that, CoddyKit PRO unlocks every lesson, plus interactive practice with a built-in code editor and a 24/7 AI tutor. The Swift Academy course includes 4 lessons in total.
What will I learn in “UIHostingController: SwiftUI Inside UIKit”?
Embedding SwiftUI views into UIKit apps with UIHostingController. You practise Swift Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.
Do I need any experience to start Swift Academy?
No prior experience is required. Swift Academy on CoddyKit is structured for beginners through advanced learners, so you can start here or from the beginning and move at your own pace. This is lesson 4 of 4.
How long does the “UIHostingController: SwiftUI Inside UIKit” lesson take?
Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.
Can I write and run code in this Swift Academy lesson?
Yes. Every Swift Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.
All lessons in this course
- UIViewRepresentable: Wrapping UIKit Views
- Coordinator Pattern for Delegate Communication
- UIViewControllerRepresentable
- UIHostingController: SwiftUI Inside UIKit