Home IOS Development Facet Ratios in SwiftUI · objc.io

Facet Ratios in SwiftUI · objc.io

Facet Ratios in SwiftUI · objc.io


One of many modifiers that at all times puzzled me a bit was .aspectRatio. How does it actually work? As soon as I figured it out, it turned out to be less complicated than I assumed.

One place the place we are able to discover out so much about how SwiftUI works is SwiftUI’s .swiftinterface file. That is situated inside Xcode. Inside your Terminal, go to /Purposes/Xcode.app, and carry out the next command:

								discover . -path "*/SwiftUI.framework*swiftinterface"


There are a number of variants of the .aspectRatio API, however all of them boil right down to a single implementation:

								func aspectRatio(_ aspectRatio: CGFloat?, contentMode: ContentMode) -> some View {


The variant with CGSize simply calls this methodology with measurement.width/measurement.peak, and .scaledToFit and .scaledToFill name this methodology with the respective content material modes and an aspectRatio of nil.

After we name aspectRatio with a hard and fast facet ratio, e.g. .aspectRatio(16/9, contentMode: .match), the facet ratio implementation takes the proposed measurement, and proposes a brand new measurement to its baby. When the content material mode is .match, it matches a rectangle with the specified facet ratio contained in the proposed measurement. For instance, while you suggest 100×100, it can suggest 100×56.2 to its baby. While you select .fill as an alternative, it can suggest 177.8×100 to its baby as an alternative.

I found out this conduct by printing the proposed sizes. Extra on that beneath.

Maybe the most typical use of aspectRatio is mixed with a resizable picture, like so:

    .aspectRatio(contentMode: .match)


It will draw the picture to suit inside the proposed measurement. Be aware that we don’t specify the precise facet ratio: it’s derived from the underlying picture.

After we do not specify a hard and fast facet ratio however use nil for the parameter, the facet ratio modifier seems to be on the very best measurement of the underlying view. This implies it merely proposes nil×nil to the underlying view, and makes use of the results of that to find out the facet ratio. For instance, when the picture stories its very best measurement as 100×50, the computed facet ratio is 100/50.

The method then continues like earlier than: when the view was proposed 320×480, the picture shall be sized to 320×160 when the content material mode is about to .match, and 960×480 when the content material mode is about to .fill.

Determining proposed sizes

Proposed sizes will not be a part of the general public API of SwiftUI. Though you completely want to know how this works in an effort to write efficient layouts, this is not actually documented. The one official place the place this conduct is described is within the glorious 2019 WWDC speak Constructing Customized Views with SwiftUI.

Nonetheless, there’s a hack to do that. Contained in the interface file talked about above, I looked for “ProposedSize” and located a protocol named _ArchivableView which permits us to override sizeThatFits:

								struct MySample: _ArchivableView {
    var physique: some View {
    func sizeThatFits(in proposedSize: _ProposedSize) -> CGSize {
        return proposedSize.orDefault


We are able to now merely assemble a MySample with a side ratio and print the outcome. As a substitute of a .body, you may also use .fixedSize() to suggest nil for the width and/or peak. Likewise, attempt leaving out the primary parameter and see how .aspectRatio proposes nil to determine the perfect measurement of its baby view.

    .aspectRatio(100/50, contentMode: .fill)
    .body(width: 320, peak: 480)


Sadly the width and peak properties on _ProposedSize aren’t seen within the swift interface, so I had to make use of introspection to print these (and in addition add a number of helper strategies like .fairly and .orDefault). The complete code is in a gist.

If you wish to study extra about how SwiftUI works, learn our e-book Considering in SwiftUI. When your organization is already constructing issues in SwiftUI — or is about to get began — contemplate reserving a SwiftUI Workshop in your staff.



Please enter your comment!
Please enter your name here