0Pricing
Swift Academy · Lesson

UIHostingController: SwiftUI Inside UIKit

Embedding SwiftUI views into UIKit apps with UIHostingController.

UIHostingController: SwiftUI Inside UIKit is a free Swift Academy lesson on CoddyKit — lesson 4 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the Swift Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.

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 versa

Sizing 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 = size

Appearance and Style

Set overrideUserInterfaceStyle on the hosting controller to control dark/light mode independently.

hostingVC.overrideUserInterfaceStyle = .dark

Scene-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.

Frequently asked questions

Is the “UIHostingController: SwiftUI Inside UIKit” lesson free?

Yes — the full text of “UIHostingController: SwiftUI Inside UIKit” is free to read here on the web, and the Swift Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the Swift Academy course, upgrade to CoddyKit PRO.

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; this is — lesson 4 of 4, so you can start here or from the beginning and move at your own pace.

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

  1. UIViewRepresentable: Wrapping UIKit Views
  2. Coordinator Pattern for Delegate Communication
  3. UIViewControllerRepresentable
  4. UIHostingController: SwiftUI Inside UIKit
← Back to Swift Academy