SwiftUI Views & Modifiers
Everything you see in a SwiftUI app is a view: text, images, buttons, shapes, and the containers that arrange them. You customise views with modifiers — methods like .font(), .padding() and .background() that wrap a view in a new, changed view.
Note: SwiftUI code can't run in the browser. Each SwiftUI example is followed by an HTML mock of the iPhone screen.
Basic views
| View | Shows | Example |
|---|---|---|
Text |
a string | Text("Hello") |
Image |
an image or SF Symbol icon | Image(systemName: "heart.fill") |
Button |
a tappable control | Button("Save") { save() } |
Label |
icon + text pair | Label("Inbox", systemImage: "tray") |
Toggle |
an on/off switch | Toggle("Wi-Fi", isOn: $wifi) |
TextField |
editable text | TextField("Name", text: $name) |
Divider, Spacer |
a line / flexible space | Divider() |
Rectangle, Circle, Capsule |
shapes | Circle().fill(.red) |
Modifiers
A modifier returns a new view that wraps the old one, so you can chain them:
import SwiftUI
struct ProfileCard: View {
var body: some View {
VStack(spacing: 8) {
Image(systemName: "person.crop.circle.fill")
.font(.system(size: 64))
.foregroundStyle(.orange)
Text("Lena Ortiz")
.font(.title2)
.bold()
Text("iOS Developer")
.font(.subheadline)
.foregroundStyle(.secondary)
}
.padding(24)
.background(.orange.opacity(0.12))
.clipShape(RoundedRectangle(cornerRadius: 20))
}
}On screen this becomes a soft orange card with an avatar, a bold name and a grey job title:
<div style="width:320px;height:380px;margin:12px auto;border:10px solid #1c1c1e;border-radius:44px;background:#fff;font-family:-apple-system,system-ui,'Helvetica Neue',sans-serif;display:flex;align-items:center;justify-content:center">
<div style="background:rgba(255,149,0,.12);border-radius:20px;padding:24px;text-align:center">
<div style="width:64px;height:64px;margin:0 auto 8px;border-radius:50%;background:#ff9500;color:#fff;font-size:36px;line-height:64px">👤</div>
<div style="font-size:22px;font-weight:700;margin-bottom:8px">Lena Ortiz</div>
<div style="font-size:15px;color:#8e8e93">iOS Developer</div>
</div>
</div>Order matters
Because each modifier wraps the previous result, the order changes the outcome. Padding then background colours the padded area; background then padding leaves the padding transparent:
Text("A").padding().background(.yellow) // yellow box around A with space
Text("B").background(.yellow).padding() // yellow only behind B, space outside<div style="width:320px;margin:12px auto;border:10px solid #1c1c1e;border-radius:44px;background:#fff;font-family:-apple-system,system-ui,sans-serif;padding:40px 0;display:flex;flex-direction:column;align-items:center;gap:8px;font-size:17px">
<span style="background:#ffcc00;padding:16px">A</span>
<span style="padding:16px"><span style="background:#ffcc00">B</span></span>
</div>You can model this "wrapping" idea in plain Swift — each modifier returns a new value that contains the old one:
struct Styled: CustomStringConvertible {
let description: String
func padding() -> Styled { Styled(description: "Padding(\(description))") }
func background(_ color: String) -> Styled { Styled(description: "Background[\(color)](\(description))") }
}
let a = Styled(description: "Text(A)").padding().background("yellow")
let b = Styled(description: "Text(B)").background("yellow").padding()
print(a)
print(b)Background[yellow](Padding(Text(A)))
Padding(Background[yellow](Text(B)))
Buttons
A Button takes a title (or any custom label view) and an action closure:
struct LikeButton: View {
@State private var likes = 0
var body: some View {
Button {
likes += 1
} label: {
Label("Like (\(likes))", systemImage: "hand.thumbsup.fill")
.padding(.horizontal, 20)
.padding(.vertical, 12)
.background(.blue)
.foregroundStyle(.white)
.clipShape(Capsule())
}
}
}This mock button behaves like the real one — tap it:
<div style="width:320px;height:240px;margin:12px auto;border:10px solid #1c1c1e;border-radius:44px;background:#fff;font-family:-apple-system,system-ui,sans-serif;display:flex;align-items:center;justify-content:center">
<button id="like" style="border:0;border-radius:999px;background:#007aff;color:#fff;font-size:17px;padding:12px 20px;cursor:pointer;font-family:inherit">👍 Like (0)</button>
</div>
<script>
let likes = 0;
const btn = document.getElementById('like');
btn.onclick = () => { likes += 1; btn.textContent = `👍 Like (${likes})`; };
</script>Common modifiers
| Modifier | Effect |
|---|---|
.font(.title) / .bold() |
text style |
.foregroundStyle(.red) |
text/icon colour |
.padding() / .padding(.top, 8) |
space around the view |
.frame(width:height:) |
propose a size |
.background(.blue) |
fill behind the view |
.clipShape(Circle()) |
cut to a shape |
.shadow(radius: 4) |
drop shadow |
.opacity(0.5) |
transparency |
.onTapGesture { } |
react to taps |
Tip: Modifiers you apply to a container, such as
.font(.title)on aVStack, are inherited by the children that don't set their own — an easy way to style a whole group.
Test yourself
Why does Text("Hi").padding().background(.yellow) look different from Text("Hi").background(.yellow).padding()?
- It doesn't — they are identical
- Each modifier wraps the previous view, so the order changes what gets coloured
- background() only works as the last modifier
- padding() removes the background
Which view shows an SF Symbol icon?
- Text(systemName:)
- Image(systemName:)
- Icon("star")
- Symbol("star")