Activity 1
Extract a reusable card
Build a course row, then extract its status badge and action area into focused child views. Keep the screen's data flow visible.
Hint: Extract around a stable concept, not only a line count.
SwiftUI Fundamentals
Build reusable views, understand modifier order, and design focused visual APIs with ViewBuilder composition.
Before you begin
Learning stage
SwiftUI interfaces scale through composition. A screen combines smaller views, each responsible for one meaningful visual or interaction concept. Child views receive the smallest useful inputs and emit actions through closures or bindings instead of reaching into unrelated global state.
A modifier returns a new view that wraps or transforms the previous one. Order matters because padding followed by a background gives a different result from a background followed by padding. Read a modifier chain from the base view outward, one transformation at a time.
The ViewBuilder result builder allows body and container closures to combine multiple child views and use limited control flow such as if and switch. Stable structure and identity still matter; avoid hiding large unrelated screen branches inside deeply nested builders.
Learning stage
The card owns its presentation while its caller supplies content and the action to perform.
import SwiftUI
struct LessonCard: View {
let title: String
let minutes: Int
let isComplete: Bool
let onOpen: () -> Void
var body: some View {
Button(action: onOpen) {
HStack(spacing: 14) {
Image(systemName: isComplete ? "checkmark.circle.fill" : "book")
.foregroundStyle(isComplete ? .green : .blue)
VStack(alignment: .leading, spacing: 4) {
Text(title).font(.headline)
Text("(minutes) min").font(.subheadline).foregroundStyle(.secondary)
}
Spacer()
Image(systemName: "chevron.right")
.foregroundStyle(.tertiary)
}
.padding()
.background(.background, in: .rect(cornerRadius: 16))
.overlay {
RoundedRectangle(cornerRadius: 16).stroke(.separator)
}
}
.buttonStyle(.plain)
}
}Learning stage
Connect the SwiftUI concept to complete, maintainable application features.
Learning stage
Identify patterns that weaken ownership, identity, adaptation, accessibility, or user experience.
Learning stage
Build the behavior in a small Xcode project and review it in multiple states.
Activity 1
Build a course row, then extract its status badge and action area into focused child views. Keep the screen's data flow visible.
Hint: Extract around a stable concept, not only a line count.
Activity 2
Create two Text views: padding then background, and background then padding. Describe the resulting visual boundaries.
Hint: Add a border after each step to make the transformed frame visible.
Learning stage
Answer each question before revealing the explanation.
QUESTION 1
A new transformed view value
Modifier chains build nested view descriptions, so order can affect layout, drawing, and behavior.
QUESTION 2
The smallest meaningful values and actions required to render and behave correctly.
Focused inputs reduce coupling and make previews, reuse, and testing easier.
Learning stage
Explain the design in terms of data flow, identity, lifecycle, platform behavior, and trade-offs.
Short answer: Each modifier transforms the view produced before it, creating nested layout and drawing behavior.
For example, background then padding colors only the original content area, while padding then background colors the padded area. Some modifiers affect layout proposals, others draw, add gestures, or alter environment values, but all participate at a specific point in the chain.
Possible follow-ups
Learning stage
Compose interfaces from semantic views with focused inputs. Treat modifier chains as ordered transformations and keep data ownership outside reusable presentation components unless the state is truly local.