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

Understanding SwiftUI’s Layout System: Stacks, Frames, and Alignment

2/2/2025

0 Comments

 
Picture
​Welcome to Week 5 of the SwiftUI Blog Series! By now, you’ve mastered the basics of SwiftUI and its Previews. This week, we’re diving deep into SwiftUI’s layout system, which is the foundation for designing beautiful and responsive user interfaces.
​
We’ll explore three essential building blocks of layouts in SwiftUI: Stacks, Frames, and Alignment. By the end of this blog, you’ll be able to craft pixel-perfect UIs for any screen size.

​How SwiftUI Handles Layout

SwiftUI’s layout system is declarative and hierarchy-driven. Every view decides its own size and position based on its parent and child views. This two-step process makes layouts dynamic and responsive:
  1. Parent Views propose a size to their child views.
  2. Child Views decide their size and return it to the parent.

With this foundation, let’s dive into the tools SwiftUI offers for creating layouts.

​Stacks: The Foundation of SwiftUI Layouts

​Stacks are the most fundamental layout containers in SwiftUI. They arrange child views in a straight line, either vertically, horizontally, or in depth.
1. HStack (Horizontal Stack)
An HStack arranges views in a horizontal line:

    
Key Modifiers for HStack:
  • spacing: Adjusts the space between views.
  • alignment: Aligns views along a vertical axis.

    
2. VStack (Vertical Stack)
A VStack arranges views in a vertical line:

    
​Alignment in VStack:
Align views horizontally using the alignment parameter.

    
3. ZStack (Depth Stack)
A ZStack overlays views on top of each other:

    
Customizing ZStack:
Use alignment to position views within the stack.

    

​Frames: Controlling Size and Position

​The frame() modifier lets you control the size and position of a view.
Syntax

    
Examples
  • Set Fixed Dimensions:

    
  • Expand to Fill Parent:

    
Alignment within Frames
The alignment parameter controls how the content is positioned inside the frame:

    
Alignment Guides: Precision Positioning
For advanced control, SwiftUI provides alignment guides, allowing fine-grained positioning of views.

    
​This offsets the alignment points of individual views.
Practical Example: A Profile Card
Let’s put everything together to create a Profile Card UI:

    
Picture
This profile card uses a ZStack for the background and overlays, a VStack for vertical content, and a frame to define its size.

​Key Takeaways

  1. Stacks are the foundation of layouts in SwiftUI: HStack, VStack, and ZStack.
  2. Use frames to control size and positioning.
  3. Alignment guides give fine-grained control over view positioning.

By mastering these tools, you can build sophisticated and responsive layouts in SwiftUI.

​What’s Next?

​In Week 6, we’ll explore Mastering Grids in SwiftUI, including how to use LazyVGrid and LazyHGrid to create complex, scrollable layouts efficiently. Stay tuned!
0 Comments

Your comment will be posted after it is approved.


Leave a Reply.

    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