10.4 C
Canberra
Friday, September 20, 2024

Side 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 believed.

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

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

							

There are a couple 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 technique with dimension.width/dimension.top, and .scaledToFit and .scaledToFill name this technique with the respective content material modes and an aspectRatio of nil.

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

I discovered this conduct by printing the proposed sizes. Extra on that under.

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

								Picture("check")
    .resizable()
    .aspectRatio(contentMode: .match)

							

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

After we do not specify a set facet ratio however use nil for the parameter, the facet ratio modifier appears to be like on the splendid dimension 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 splendid dimension 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 aren’t a part of the general public API of SwiftUI. Despite the fact that you completely want to grasp how this works so as to write efficient layouts, this is not actually documented. The one official place the place this conduct is described is within the wonderful 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 {
        Rectangle()
    }
    
    func sizeThatFits(in proposedSize: _ProposedSize) -> CGSize {
        print(proposedSize.fairly)
        return proposedSize.orDefault
    }
}

							

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

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

							

Sadly the width and top properties on _ProposedSize aren’t seen within the swift interface, so I had to make use of introspection to print these (and likewise add a couple 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 ebook Considering in SwiftUI. When your organization is already constructing issues in SwiftUI — or is about to get began — contemplate reserving a SwiftUI Workshop to your workforce.

Related Articles

LEAVE A REPLY

Please enter your comment!
Please enter your name here

[td_block_social_counter facebook="tagdiv" twitter="tagdivofficial" youtube="tagdiv" style="style8 td-social-boxed td-social-font-icons" tdc_css="eyJhbGwiOnsibWFyZ2luLWJvdHRvbSI6IjM4IiwiZGlzcGxheSI6IiJ9LCJwb3J0cmFpdCI6eyJtYXJnaW4tYm90dG9tIjoiMzAiLCJkaXNwbGF5IjoiIn0sInBvcnRyYWl0X21heF93aWR0aCI6MTAxOCwicG9ydHJhaXRfbWluX3dpZHRoIjo3Njh9" custom_title="Stay Connected" block_template_id="td_block_template_8" f_header_font_family="712" f_header_font_transform="uppercase" f_header_font_weight="500" f_header_font_size="17" border_color="#dd3333"]
- Advertisement -spot_img

Latest Articles