Hamdouchi Interactive
  • Welcome
  • Mobile
  • Tech Blog
    • Swift
    • SwiftUI
  • Let's Talk

Unlocking the Power of SwiftUI:
A Tech Blog Series

Welcome to our blog series focused on the exciting world of SwiftUI! Here, we aim to share valuable insights, practical tips, and deep dives into the latest SwiftUI features and techniques. From crafting seamless user interfaces to exploring performance strategies, our articles are designed to inspire and empower developers of all levels. Stay connected as we explore the endless possibilities with SwiftUI and spark engaging discussions about building modern, efficient apps.

Explore all SwiftUI Topics

Responsive Design in SwiftUI: Adapting to All Screen Sizes

2/23/2025

0 Comments

 
Picture
With iPhones, iPads, Macs, and even Apple Watches running SwiftUI, designing an app that adapts to different screen sizes is crucial. A layout that looks perfect on an iPhone 15 Pro might break on an iPad or Mac — unless you use SwiftUI’s responsive design techniques.
​
In this blog, we’ll explore how to:
  • Make layouts adaptive across devices.
  • Use GeometryReader to build flexible UIs.
  • Implement dynamic type and scaling for accessibility.
  • Work with size classes and device orientation.

​Let’s make your SwiftUI apps look great everywhere! ✨

​Using Frames and Flexible Layouts

​A fixed-width UI can break on different screen sizes. Instead, use flexible frames:

❌ Incorrect Approach (Fixed Size)

    
​✅ Better Approach (Flexible Layout)

    
Why This Works:
  • .maxWidth(.infinity) lets the view expand naturally.
  • The background stretches dynamically to fit.
  • Works great across iPhone, iPad, and Mac.

​Adaptive Grids for Different Devices

​Using LazyVGrid with .adaptive(minimum:) makes sure your app’s grid adjusts dynamically:

    
Why This Works:
  • On iPhone → Fewer columns appear.
Picture
  • On Mac → Wider layout, no wasted space.
Picture
  • On iPad → More columns fit naturally.
Picture

​Using GeometryReader for Dynamic Resizing

​GeometryReader lets you dynamically size views based on screen dimensions.

    
iPhone Preview
Picture
iPad Preview
Picture
​How This Helps:
  • Reads the screen’s width and height in real time.
  • Adjusts layouts dynamically without hardcoding values.
  • Essential for split-screen and multi-window apps.

​Supporting Dynamic Type and Accessibility Scaling

​Accessibility is a must in modern apps. Supporting Dynamic Type ensures your text scales properly.

    
How This Works:
  • minimumScaleFactor(0.5): Prevents text from getting clipped.
  • lineLimit(1): Ensures no unwanted wrapping.
  • Text automatically scales for different font settings.

​Detecting Size Classes for iPhone vs. iPad Layouts

​Use horizontalSizeClass and verticalSizeClass to adjust UI for compact (iPhone) vs. regular (iPad) screens.

    
Why This Works:
  • sizeClass == .compact → Adjusts UI for iPhones.
  • sizeClass == .regular → Adjusts UI for iPads and Macs.
  • Ensures optimal user experience across all devices.

Handling Device Rotation and Orientation Changes

​Use @Environment(\.verticalSizeClass) to detect portrait vs. landscape mode:

    
Why This Matters:
  • Adapts UI when users rotate their device.
  • Works great for multimedia, games, and split-screen apps.

​Building a Responsive Dashboard

​Let’s put everything together to build a responsive dashboard that adapts based on device size!

    
iPhone Preview:
Picture
iPad Preview:
Picture
Features of This Dashboard:
  • Automatically switches between VStack (iPhone) and HStack (iPad).
  • Uses environment size classes for real-time layout adaptation.
  • Shadows, padding, and corner radius for a sleek UI.

​Key Takeaways

  • Use .frame(maxWidth: .infinity) for flexible layouts.
  • Lazy grids and adaptive columns make UI responsive.
  • GeometryReader dynamically adjusts UI based on screen size.
  • Size classes and device orientation help tailor UI for iPhone, iPad, and Mac.

​Master these techniques, and your SwiftUI apps will look great on ANY Apple device!

​What’s Next?

​Next week, we’ll explore State Management Best Practices in Large SwiftUI Apps — how to structure your app’s state efficiently while keeping performance smooth!
​
Stay tuned and happy coding!
0 Comments
<<Previous
Forward>>
    Picture

    ​Mohamed Hamdouchi


    Author

    Lead iOS Engineer.
    I help develop Design System Libraries.
    Creative Thinker.
    Featured on the App Store with 4M+ downloads.

    Archives

    March 2025
    February 2025
    January 2025

    Categories

    All
    Alignment
    Binding
    EnvironmentObject
    Frames
    Grids
    Layout
    List
    Modifiers
    ObservedObject
    Overlay
    Previews
    Responsive Design
    Stacks
    State
    State Management
    StateObject
    Task Manager
    Views
    ZStack

​​COPYRIGHT © 2009 HAMDOUCHI INTERACTIVE, LLC. ​ALL RIGHTS RESERVED
  • Welcome
  • Mobile
  • Tech Blog
    • Swift
    • SwiftUI
  • Let's Talk