Container Relative Frame
Proportional layouts in SwiftUI
In a previous article, I’ve presented the use of the Layout protocol in SwiftUI by creating a custom proportional layout which can be used to size views proportionally to their container. The results of that layout were rather nice, however in iOS 17, Apple introduced a native solution in SwiftUI, containerRelativeFrame
The modifier
The container relative frame modifier has two variants
containerRelativeFrame(.horizontal, count: 3, span: 1, spacing: 0)
// and
containerRelativeFrame(.horizontal) { length, axis in length * 0.75 }
The first one can be used to size the view proportionally. Count is the number of imaginary columns the container is divided into while span is the number of columns the view will occupy. Spacing tells the modifier how much space to subtract from the length, but doesn’t actually introduce any space between views. That has to be controlled from the container
The second version takes a callback, in which you can perform any custom logic to determine the final size based on the container size.
The callback also receives the axis, so if you specify two axis in the first parameter, the callback will be executed twice and you can perform different logic for different axis
But what is a container?
The only limitation of the container relative frame is that you can’t control what is a container. According to apple's documentation, at the time of writing this article, a container can be the window presenting the view, a scroll view or any navigation component (split view column, tab view frame, etc)
Full example
Let's see a full example
ScrollView {
HStack {
Color.green
.containerRelativeFrame(.horizontal, count: 3, span: 1, spacing: 0)
Color.red
.containerRelativeFrame(.horizontal, count: 4, span: 1, spacing: 0)
Color.green
.containerRelativeFrame(.horizontal) { length, _ in length * 0.75 }
}
}
Related articles
Here are some more articles that may interest you. Check them out!
The TimelineView
published on November 2, 2025
SwiftUILearn how to use SwiftUI’s TimelineView to build time‑based animations. This article explains scheduler options, shows practical examples (pauseable clock, smooth minute‑progress ring with hue shifts and tick marks), and helps you choose the right schedule for smooth or precise updates.
Read moreMatched Transitions in SwiftUI
published on September 23, 2025
SwiftSwiftUIAnimationsLearn how to use matchedGeometryEffect and navigationTransition in SwiftUI to create smooth, visually engaging transitions between views and screens. This article covers practical examples for synchronizing view geometry and implementing the new zoom navigation transition, helping you build more dynamic and polished UIs.
Read moreSwiftUI Transitions
published on September 7, 2025
SwiftSwiftUIAnimationsLearn how to create smooth, engaging UI animations in SwiftUI using built-in and custom transitions. From simple fades to complex combinations, this guide covers the essentials with best practices and examples.
Read more