SwiftUI Layouts and Stacks

Learn About VStack, HStack, ZStack, and how to Leverage GeometryReader

Pavlos Simas
3 min readAug 2, 2023
Photo by AltumCode on Unsplash

We will dive deep into SwiftUI layouts and explore the power of stacks in organizing views within your app’s user interface. You’ll learn about VStack, HStack, ZStack, and how to leverage GeometryReader and Spacer to create responsive and adaptive UIs.

Understanding SwiftUI Layouts

In SwiftUI, layouts are an essential aspect of building user interfaces. They determine how views are arranged and positioned within the app’s screen. The most common layout components in SwiftUI are VStack, HStack, and ZStack.

a. VStack (Vertical Stack):
The VStack arranges views vertically, from top to bottom.

import SwiftUI

struct ContentView: View {
var body: some View {
VStack(alignment: .leading, spacing: 10) {
Text("First View")
Text("Second View")
Text("Third View")
}
}
}

struct ContentView_Previews: PreviewProvider {
static var previews: some View {
ContentView()
}
}

b. HStack (Horizontal Stack):
The HStack arranges views horizontally, from leading to trailing.

import SwiftUI

struct ContentView: View {
var body: some View {…

--

--

Pavlos Simas

iOS Developer, with passion about Development, Marketing and Finance. Join Medium from the following link: https://simaspavlos.medium.com/membership