Close Menu
geekfence.comgeekfence.com
    What's Hot

    refurbed and GoPro announce exclusive partnership in Ireland

    March 28, 2026

    Enterprise Network Trends & Strategy: WAN Manager Survey Insights

    March 28, 2026

    Posit AI Blog: De-noising Diffusion with torch

    March 28, 2026
    Facebook X (Twitter) Instagram
    • About Us
    • Contact Us
    Facebook Instagram
    geekfence.comgeekfence.com
    • Home
    • UK Tech News
    • AI
    • Big Data
    • Cyber Security
      • Cloud Computing
      • iOS Development
    • IoT
    • Mobile
    • Software
      • Software Development
      • Software Engineering
    • Technology
      • Green Technology
      • Nanotechnology
    • Telecom
    geekfence.comgeekfence.com
    Home»iOS Development»Aspect Ratios in SwiftUI · objc.io
    iOS Development

    Aspect Ratios in SwiftUI · objc.io

    AdminBy AdminNovember 19, 2025No Comments4 Mins Read2 Views
    Facebook Twitter Pinterest LinkedIn Telegram Tumblr Email
    Aspect Ratios in SwiftUI · objc.io
    Share
    Facebook Twitter LinkedIn Pinterest Email


    One of the modifiers that always puzzled me a bit was .aspectRatio. How does it really work? Once I figured it out, it turned out to be simpler than I thought.

    One place where we can find out a lot about how SwiftUI works is SwiftUI’s .swiftinterface file. This is located inside of Xcode. Inside your Terminal, go to /Applications/Xcode.app, and perform the following command:

    								find . -path "*/SwiftUI\.framework*swiftinterface"
    
    							

    There are a few variants of the .aspectRatio API, but they all boil down to a single implementation:

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

    The variant with CGSize just calls this method with size.width/size.height, and .scaledToFit and .scaledToFill call this method with the respective content modes and an aspectRatio of nil.

    When we call aspectRatio with a fixed aspect ratio, e.g. .aspectRatio(16/9, contentMode: .fit), the aspect ratio implementation takes the proposed size, and proposes a new size to its child. When the content mode is .fit, it fits a rectangle with the desired aspect ratio inside the proposed size. For example, when you propose 100×100, it will propose 100×56.2 to its child. When you choose .fill instead, it will propose 177.8×100 to its child instead.

    I figured out this behavior by printing the proposed sizes. More on that below.

    Perhaps the most common use of aspectRatio is combined with a resizable image, like so:

    								Image("test")
        .resizable()
        .aspectRatio(contentMode: .fit)
    
    							

    This will draw the image to fit within the proposed size. Note that we do not specify the actual aspect ratio: it is derived from the underlying image.

    When we don’t specify a fixed aspect ratio but use nil for the parameter, the aspect ratio modifier looks at the ideal size of the underlying view. This means it simply proposes nil×nil to the underlying view, and uses the result of that to determine the aspect ratio. For example, when the image reports its ideal size as 100×50, the computed aspect ratio is 100/50.

    The process then continues like before: when the view was proposed 320×480, the image will be sized to 320×160 when the content mode is set to .fit, and 960×480 when the content mode is set to .fill.

    Figuring out proposed sizes

    Proposed sizes are not part of the public API of SwiftUI. Even though you absolutely need to understand how this works in order to write effective layouts, this isn’t really documented. The only official place where this behavior is described is in the excellent 2019 WWDC talk Building Custom Views with SwiftUI.

    However, there is a hack to do this. Inside the interface file mentioned above, I searched for “ProposedSize” and found a protocol named _ArchivableView which allows us to override sizeThatFits:

    								struct MySample: _ArchivableView {
        var body: some View {
            Rectangle()
        }
        
        func sizeThatFits(in proposedSize: _ProposedSize) -> CGSize {
            print(proposedSize.pretty)
            return proposedSize.orDefault
        }
    }
    
    							

    We can now simply construct a MySample with an aspect ratio and print the result. Instead of a .frame, you can also use .fixedSize() to propose nil for the width and/or height. Likewise, try leaving out the first parameter and see how .aspectRatio proposes nil to figure out the ideal size of its child view.

    								MySample()
        .aspectRatio(100/50, contentMode: .fill)
        .frame(width: 320, height: 480)
    
    							

    Unfortunately the width and height properties on _ProposedSize aren’t visible in the swift interface, so I had to use introspection to print those (and also add a few helper methods like .pretty and .orDefault). The full code is in a gist.

    If you want to learn more about how SwiftUI works, read our book Thinking in SwiftUI. When your company is already building things in SwiftUI — or is about to get started — consider booking a SwiftUI Workshop for your team.



    Source link

    Share. Facebook Twitter Pinterest LinkedIn Tumblr Email

    Related Posts

    ios – What is the correct way to initialize a Data property in a model object in SwiftData / CloudKit?

    March 28, 2026

    Ultimate UICollectionView guide with iOS examples written in Swift

    March 24, 2026

    app store – Guideline 3.1.1 – Business – Payments – In-App Purchase in ios reject

    March 23, 2026

    More Updates from the Swift Workshop

    March 19, 2026

    How to use iCloud drive documents?

    March 18, 2026

    ios – Video input to Shortcuts action

    March 17, 2026
    Top Posts

    Understanding U-Net Architecture in Deep Learning

    November 25, 202527 Views

    Hard-braking events as indicators of road segment crash risk

    January 14, 202624 Views

    Redefining AI efficiency with extreme compression

    March 25, 202619 Views
    Don't Miss

    refurbed and GoPro announce exclusive partnership in Ireland

    March 28, 2026

    refurbed, Ireland’s leading online marketplace for refurbished goods, has launched an exclusive new partnership with…

    Enterprise Network Trends & Strategy: WAN Manager Survey Insights

    March 28, 2026

    Posit AI Blog: De-noising Diffusion with torch

    March 28, 2026

    The Path to Agentic-Ready Data: Takeaways from the Gartner Data & Analytics Summit

    March 28, 2026
    Stay In Touch
    • Facebook
    • Instagram
    About Us

    At GeekFence, we are a team of tech-enthusiasts, industry watchers and content creators who believe that technology isn’t just about gadgets—it’s about how innovation transforms our lives, work and society. We’ve come together to build a place where readers, thinkers and industry insiders can converge to explore what’s next in tech.

    Our Picks

    refurbed and GoPro announce exclusive partnership in Ireland

    March 28, 2026

    Enterprise Network Trends & Strategy: WAN Manager Survey Insights

    March 28, 2026

    Subscribe to Updates

    Please enable JavaScript in your browser to complete this form.
    Loading
    • About Us
    • Contact Us
    • Disclaimer
    • Privacy Policy
    • Terms and Conditions
    © 2026 Geekfence.All Rigt Reserved.

    Type above and press Enter to search. Press Esc to cancel.