SwiftUI Basics: Views, Modifiers, Stacks and Your First Screen
About the code in this lesson SwiftUI runs only on Apple platforms with Xcode, so the SwiftUI code here is shown without output and was not run in this course's test setup. Build and run it in Xcode to see the result.
What Is SwiftUI?
SwiftUI is Apple's framework for building interfaces by describing what the screen should look like. You write small View structs, and SwiftUI draws and updates them.
Your First View
swift
import SwiftUI
struct ContentView: View {
var body: some View {
Text("Hello, Swift!")
.font(.largeTitle)
.foregroundStyle(.blue)
}
}
body is the one required property. It returns some View, the opaque type you learned earlier. Each .font and .foregroundStyle is a modifier that returns a new, changed view.
Modifier Order Matters
swift
Text("Hi")
.padding()
.background(.yellow) // yellow covers the text plus padding
Text("Hi")
.background(.yellow) // yellow covers only the text
.padding()
Stacks
| View | Arranges children |
|---|---|
| VStack | Top to bottom |
| HStack | Left to right |
| ZStack | Layered, back to front |
| Spacer | Flexible empty space |
swift
struct ProfileCard: View {
var body: some View {
HStack(spacing: 12) {
Image(systemName: "person.circle.fill")
.font(.system(size: 48))
VStack(alignment: .leading) {
Text("Asha").font(.headline)
Text("iOS Developer").font(.subheadline).foregroundStyle(.secondary)
}
Spacer()
Button("Follow") { print("tapped") }
.buttonStyle(.borderedProminent)
}
.padding()
}
}
Previews
swift
#Preview {
ProfileCard()
}
Learn by changing In Xcode, change a modifier and watch the preview update. This is the fastest way to learn SwiftUI.
Common Mistakes
- Putting a modifier before another when the order changes the look
- Making one huge
bodyinstead of small views - Forgetting that views are structs, not classes
Practice
- Build a card with an image, a title and a subtitle
- Add a
Spacerto push a button to the right - Swap the order of
paddingandbackgroundand compare
Conclusion
SwiftUI screens are small structs with a body. Modifiers and stacks are your main tools.
Quick Summary: View; body; modifiers; stacks; previews.

