← All courses

Layout with Auto Layout and Stacks

🗓 May 31, 2026 ⏱ 1 min read

SwiftUI stacks

Arrange views with three containers: VStack (vertical), HStack (horizontal) and ZStack (layered).

VStack(spacing: 12) {
    Text("Profile").font(.title)
    HStack {
        Image(systemName: "person.circle")
        Text("Anand Gaur")
    }
}
.padding()

UIKit Auto Layout

UIKit positions views with constraints (rules like “pin to the left with 16pt”). You can set them in Interface Builder or in code:

label.translatesAutoresizingMaskIntoConstraints = false
NSLayoutConstraint.activate([
    label.topAnchor.constraint(equalTo: view.topAnchor, constant: 40),
    label.centerXAnchor.constraint(equalTo: view.centerXAnchor)
])
Rule of thumb: Never use fixed pixel positions. Use stacks/constraints so your UI adapts to every iPhone and iPad.