TechnologiesSwiftUI

UIHostingController class

iOSmacOStvOSwatchOSvisionOS✓ renders

A UIKit view controller that manages a SwiftUI view hierarchy.

How it works

A UIHostingController is a UIViewController subclass that hosts a SwiftUI view hierarchy inside a UIKit interface. It acts as the bridge between the two frameworks: you hand it a SwiftUI view as its root content, and it manages a backing view that participates in UIKit's view-controller machinery — view lifecycle, layout, and navigation. Reach for it whenever you need to drop SwiftUI into an app that's still driven by UIKit, such as pushing a SwiftUI screen onto a UINavigationController or embedding one as a child view controller.

  1. Wrap a SwiftUI view with init(rootView:)

    The primary initializer takes any value conforming to View and stores it as the controller's content. Here you pass an instance of the UIHostingControllerDemo view, and the hosting controller renders its body — the VStack of Image and Text — as the controller's view.

  2. Read and replace content through the rootView property

    The hosted view is exposed as the rootView property, which you can both read and assign after creation. Reassigning rootView swaps the displayed SwiftUI content in place, so the same UIHostingController can show a different UIHostingControllerDemo configuration without creating a new controller.

  3. Present it like any UIViewController

    Because UIHostingController is a UIViewController, you use it through standard UIKit APIs — present(_:animated:), pushViewController(_:animated:), or adding it as a child controller. The SwiftUI content, including the .padding() applied to the VStack, lays out within the controller's managed view.

  4. Let the hosted view drive sizing

    The hosting controller measures the intrinsic size of its SwiftUI content and exposes it to UIKit's layout system through the view's intrinsicContentSize and Auto Layout. The .font(.headline) and .multilineTextAlignment(.center) modifiers on the Text views shape that measured content, which the controller then fits into its UIKit container.

Try it — Change the rootView's Text("Hosted SwiftUI") to a longer string and observe how the UIHostingController resizes its managed view to fit the new SwiftUI content.

Example & preview

Press Run live & edit to compile it in your browser — then edit the Swift on the left and the preview re-renders live.

UIHostingController.swift
struct UIHostingControllerDemo: View {
    var body: some View {
        VStack(spacing: 12) {
            Image(systemName: "swift")
                .font(.largeTitle)
                .foregroundStyle(.orange)
            Text("Hosted SwiftUI")
                .font(.headline)
            Text("This view is the rootView of a UIHostingController, bridging SwiftUI into a UIKit hierarchy.")
                .font(.caption)
                .multilineTextAlignment(.center)
                .foregroundStyle(.secondary)
        }
        .padding()
    }
}
Live preview
Hosted SwiftUI This view is the rootView of a UIHostingController, bridging SwiftUI into a UIKit hierarchy.
Hosted SwiftUI This view is the rootView of a UIHostingController, bridging SwiftUI into a UIKit hierarchy.
swift → lexer → parser → sema → uiir → canvas Open in Studio ↗
What's new in SwiftUI 27 →