TechnologiesSwiftUI

UIHostingControllerSizingOptions struct

iOSmacOStvOSwatchOSvisionOS✓ renders

Options for how a hosting controller tracks its content's size.

How it works

UIHostingControllerSizingOptions is an option set that tells a UIHostingController how to derive its own size from the SwiftUI view it hosts. By default, a hosting controller doesn't propagate its content's ideal size to UIKit's layout system, so you have to constrain or measure it yourself. Setting sizingOptions on the controller opts in to self-sizing, letting the SwiftUI content's intrinsic measurement drive the controller's frame or Auto Layout. Reach for it whenever you embed SwiftUI inside a UIKit container — a cell, a stack view, or a child view controller — and want the bridge to size to fit automatically.

  1. Build content that has an intrinsic size

    Self-sizing only works if the hosted view actually reports an ideal size. Lay out content whose dimensions are determined by its subviews rather than by an external frame — for example the VStack of Text here, which measures the combined height of its .headline and .subheadline lines plus .padding().

  2. Set the sizingOptions on the hosting controller

    UIHostingControllerSizingOptions is assigned to a UIHostingController's sizingOptions property, typically right after you create it. As noted in the example, configuring .sizingOptions = [.intrinsicContentSize] is what switches the controller from a fixed-frame bridge into one that grows to fit its SwiftUI content.

  3. Choose the right option member

    Because it's an OptionSet, you compose the behavior from named members in an array literal. .intrinsicContentSize makes the controller's view report an intrinsicContentSize so Auto Layout constraints can resolve against it, while the preferred-content-size members feed UIKit's content-size channel; combine them when a presentation or container needs both.

  4. Let the option propagate the measured size

    Once the option is set, every change to the SwiftUI content re-publishes a new size up to UIKit. The .fixedSize(horizontal: false, vertical: true) modifier in the example pins the content's vertical measurement so the value the option propagates is stable and honest about how tall the stack wants to be.

Try it — Replace the second Text string with a much longer paragraph and watch the hosting controller's height grow, because .sizingOptions = [.intrinsicContentSize] re-derives the controller's size from the taller 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.

UIHostingControllerSizingOptions.swift
struct UIHostingControllerSizingOptionsDemo: View {
    // UIHostingControllerSizingOptions configures a UIHostingController to
    // self-size from its SwiftUI content. This preview shows the kind of
    // intrinsically-sized content that self-sizing measures.
    var body: some View {
        VStack(alignment: .leading, spacing: 8) {
            Text("Self-Sizing Content")
                .font(.headline)
            Text("A UIHostingController with .sizingOptions = [.intrinsicContentSize] grows to fit this stack.")
                .font(.subheadline)
                .foregroundStyle(.secondary)
        }
        .padding()
        .background(.quaternary, in: RoundedRectangle(cornerRadius: 12))
        .fixedSize(horizontal: false, vertical: true)
        .padding()
    }
}
Live preview
Self-Sizing Content A UIHostingController with .sizingOptions = [.intrinsicContentSize] grows to fit this stack.
Self-Sizing Content A UIHostingController with .sizingOptions = [.intrinsicContentSize] grows to fit this stack.
swift → lexer → parser → sema → uiir → canvas Open in Studio ↗
What's new in SwiftUI 27 →