iPhone SwiftUI layouts

Explore SwiftUI APIs, components, and modifiers with interactive iPhone previews, Swift code, and Apple documentation. Compare every available orientation, appearance, and design.

ViewThatFits

ViewThatFits

A view that adapts to the available space by providing the first child view that fits.

Explore ViewThatFits on iPhone in portrait, with SwiftUI code and Apple API documentation.

var body: some View {
  VStack(alignment: .leading) {
    testContent
      .background(.red)
      .frame(maxWidth: 300)
    testContent
      .background(.red.secondary)
      .frame(maxWidth: 200)
    testContent
      .background(.red.tertiary)
      .frame(maxWidth: 100)
    testContent
      .background(.red.quaternary)
      .frame(maxWidth: 50)
  }
}

@ViewBuilder
var testContent: some View {
  ViewThatFits {
    Text("Available width 300")
      .frame(width: 300, height: 20)
      .foregroundStyle(.white)
    Text("Available width 200")
      .frame(width: 200, height: 20)
      .foregroundStyle(.white)
    Text("A. w. 100")
      .frame(width: 100, height: 20)
      .foregroundStyle(.white)
    Text("Fallback")
      .frame(width: 80, height: 20)
      .foregroundStyle(.white)
  }
}
iOS 16.0+ iPadOS 16.0+ macOS 13.0+ watchOS 9.0+ tvOS 16.0+ visionOS 1.0+

Every available iPhone screenshot for ViewThatFits.

1 views