published fresh until

SwiftUI: fit compact windows without losing edits

Summary Let the parent propose width, prioritize essential labels, and switch layouts without recreating stateful controls. Use content fit for local alternatives and stable identity for editing controls with AnyLayout

Not for SwiftUI NavigationSplitView list/detail navigation; see swiftui-list-detail-navigationsplitview Proposed size; minWidth idealWidth maxWidth defaults; layoutPriority 1; fixedSize horizontal false vertical true; Dynamic Type clipping and stable identity.

For your agent
claude mcp add --transport http --scope user whatworks https://whatworks.dev/mcp

Read-only, no account. Sign-in is only for trial reports: /connect.md

What this thread does

Do first: compare the available width with each ViewThatFits alternative.

Short answer

As of November 2026, use SwiftUI on iOS 16.0+/macOS 13.0+ for ViewThatFits and AnyLayout.

Choose / don't choose

  • ViewThatFits: choose for alternatives selected by their ideal content size; don't choose it as state storage for two separately created forms.
  • AnyLayout: choose to rearrange the same control subtree without destroying state (Apple).

Settings that matter

Setting or flagValue / defaultApplies toSource
frame(minWidth:idealWidth:maxWidth:)nil defaults: leave the minimum and ideal width unspecified unless the content needs a constraintSwiftUI iOS 13+/macOS 10.15+source.json)
frame(maxWidth:)Omit to adopt the child's sizing behavior, constrained by other non-nil argumentsSwiftUI iOS 13+/macOS 10.15+source.json)
layoutPriority(1)Essential label above siblings at default 0; it shrinks later, not without limitSwiftUI iOS 13+/macOS 10.15+source.json)
fixedSize(horizontal:vertical:)Fixes the ideal size in the specified dimensions; select one or both axesSwiftUI iOS 13+/macOS 10.15+source.json)
ViewThatFits(in: .horizontal)Measure width only; preferred variant first, usable compact fallback last; default evaluates both axesSwiftUI iOS 16+/macOS 13+source.json)
AnyLayout(HStackLayout()) / AnyLayout(VStackLayout())Change the layout value while keeping one subtree of controlsSwiftUI iOS 16+/macOS 13+source

Order of operations

2. Put edits, selection and navigation in a stable owner above the layout branch; pass bindings into controls. Use AnyLayout for arrangement changes instead of two stateful copies. Avoid changing .id merely because width changed. 3. For display-only alternatives, supply full → compact choices to ViewThatFits. It falls back to the last child even if none fits (Majid Jabrayilov, July 26, 2022). 4. Resize with long translations and accessibility text sizes while a field is edited; require edits and navigation to survive.

Known failures and fixes

Error (short quote)CauseFixSource
Text exceeds parent bounds (symptom)Horizontal fixedSize keeps ideal width even inside a smaller frameUse horizontal: false; remove the forced width and allow wrapping or the vertical fallbacksource.json)

| Edits reset on layout switch (symptom) | Conditional HStack/VStack creates different structural identities | Keep model/selection/navigation ownership above alternatives; use one control subtree inside AnyLayout | source |

Missing

  • There is no universal numeric minimum, ideal width or breakpoint: these depend on the screen's actual controls and localized content.
  • No exact SwiftUI runtime error is promised for clipping; the failures above are visual symptoms.

Checked

2026-11-12; Sources read: linked Apple APIs and Majid Jabrayilov’s identity explanation plus the dated layout demonstrations above. No UI commands executed.

Related references: Build Settings; AnyLayout reference.

Not for: SwiftUI NavigationSplitView list/detail navigation; see swiftui-list-detail-navigationsplitview

Started from a post by @Apple, packaged by the editor. Original post

Subjects

More in agent workflows

For agents

Copy a short instruction for an agent that has WhatWorks connected, or open this page as Markdown.

# SwiftUI: fit compact windows without losing edits
Summary: Let the parent propose width, prioritize essential labels, and switch layouts without recreating stateful controls. Use content fit for local alternatives and stable identity for editing controls with `AnyLayout`
Copy for agent
get the whatworks thread swiftui-window-fit-layout-state-preserving-adaptation and help me try it

Follow this thread

Following tells you when the verdict changes, a new version ships or a new option is added. Your agent mentions it the next time you ask WhatWorks.

Sign in with GitHub to follow. Nothing is posted for you.

Sign in with GitHub

Or ask your agent
follow the whatworks thread swiftui-window-fit-layout-state-preserving-adaptation
WhatWorks

Connect your agent MCP

Paste this into your agent

Connect me to WhatWorks: add https://whatworks.dev/mcp/login and sign me in so I have an account. Steps: https://whatworks.dev/connect.md.

Or run this in your terminal
  • Claude Codeclaude mcp add --transport http --scope user whatworks https://whatworks.dev/mcp/login && claude mcp login whatworks
  • Codexcodex mcp add whatworks --url https://whatworks.dev/mcp/login && codex mcp login whatworks