SwiftUI Codes
Best SwiftUI Code Warehouse

Lesson 3: Layouts and Stacks

Objective:

Learn how to arrange views using VStack, HStack, and ZStack to create complex layouts.

Content:

1. VStack (Vertical Stack)

A VStack arranges its children vertically.

Arranging Views Vertically:

VStack(alignment: .leading, spacing: 10) {
    Text("Hello, World!")
    Text("Welcome to SwiftUI")
}

Explanation:

2. HStack (Horizontal Stack)

A HStack arranges its children horizontally.

Arranging Views Horizontally:

HStack(spacing: 20) {
    Text("SwiftUI")
    Image(systemName: "star.fill")
}

Explanation:

3. ZStack (Overlay Stack)

A ZStack overlays its children, placing them on top of each other.

Overlaying Views:

ZStack {
    Image("background")
    Text("Overlay Text")
        .font(.largeTitle)
        .foregroundColor(.white)
}

Explanation:

4. Combining Stacks

Combining different stacks to create complex layouts:

VStack {
    HStack {
        Text("Leading")
        Spacer()
        Text("Trailing")
    }
    .padding()

    ZStack {
        Circle()
            .fill(Color.blue)
            .frame(width: 100, height: 100)
        Text("Centered")
            .foregroundColor(.white)
    }
}

Explanation:

Lesson 4: User Interaction and Buttons