SwiftUI Intro
SwiftUI is Apple's framework for building user interfaces on iPhone, iPad, Mac, Apple Watch, Apple TV and Vision Pro — all with the same Swift code. Instead of creating buttons and labels one by one and updating them by hand, you describe what the screen should look like for the current data, and SwiftUI keeps the screen in sync whenever that data changes.
Note: SwiftUI needs Apple's platforms (Xcode on a Mac), so it cannot run in this browser. In these lessons the SwiftUI code is shown in a normal code block, followed by a live HTML mock of what the iPhone screen looks like. Plain-Swift examples still run for real.
Declarative vs imperative
| Imperative (UIKit) | Declarative (SwiftUI) |
|---|---|
| "Create a label, set its text, add it to the view, and when the count changes, find the label and update it." | "The screen is a text showing count." |
| You manage every update yourself | The framework re-renders when state changes |
| Lots of code to keep UI and data in sync | UI is a function of the data |
Your first SwiftUI app
A new Xcode project (File › New › Project › iOS › App) gives you two files. The App struct is the entry point, and ContentView is the first screen:
import SwiftUI
@main
struct HelloApp: App {
var body: some Scene {
WindowGroup {
ContentView()
}
}
}
struct ContentView: View {
var body: some View {
VStack {
Image(systemName: "globe")
.font(.largeTitle)
.foregroundStyle(.tint)
Text("Hello, SwiftUI!")
}
.padding()
}
}Running it in the iPhone simulator shows a globe icon above a greeting, centred on the screen:
<div style="width:320px;height:440px;margin:12px auto;border:10px solid #1c1c1e;border-radius:44px;background:#fff;font-family:-apple-system,system-ui,'Helvetica Neue',sans-serif;display:flex;flex-direction:column;align-items:center;justify-content:center;gap:10px;color:#000">
<div style="font-size:40px;color:#007aff">🌐</div>
<div style="font-size:17px">Hello, SwiftUI!</div>
</div>Anatomy of a view
struct ContentView: View— every screen or component is a struct that conforms to theViewprotocol. Structs are cheap value types, so SwiftUI can create thousands of them.var body: some View— the only requirement ofView. It returns the content.some Viewmeans "a specific view type that the compiler figures out", so you don't have to spell out long generic types.VStack { … }— a container that stacks its children vertically..padding(),.font(…)— modifiers: methods that return a new, modified view.@main— marks where the app starts.
Views are just Swift values
Because a view is a plain struct with a body computed property, you can understand the model in ordinary Swift. This runnable sketch imitates SwiftUI's idea with a tiny protocol of our own:
protocol MiniView {
func render(indent: Int) -> String
}
struct Label: MiniView {
let text: String
func render(indent: Int) -> String {
String(repeating: " ", count: indent) + "Text(\"\(text)\")"
}
}
struct Column: MiniView {
let children: [MiniView]
func render(indent: Int) -> String {
let pad = String(repeating: " ", count: indent)
let inner = children.map { $0.render(indent: indent + 2) }
return ([pad + "VStack {"] + inner + [pad + "}"]).joined(separator: "\n")
}
}
let screen = Column(children: [Label(text: "Hello"), Label(text: "SwiftUI")])
print(screen.render(indent: 0))VStack {
Text("Hello")
Text("SwiftUI")
}
The real framework does the same thing in spirit: it walks the tree of view values and turns it into pixels.
Previews
Xcode can show your view live next to the code, without launching the simulator. Add a #Preview block below the view:
#Preview {
ContentView()
}Every time you edit the code, the canvas updates. You can add several previews — for example one in dark mode, or with a long name — to check edge cases quickly.
What you'll learn in this section
| Lesson | Topic |
|---|---|
| Views & Modifiers | Text, Image, Button, and styling with modifiers |
| Stacks & Layout | VStack, HStack, ZStack, Spacer, frames |
| State & Binding | @State, @Binding, and how data drives the UI |
| Lists & Navigation | List, ForEach, NavigationStack, NavigationLink |
Tip: SwiftUI requires iOS 13+, but most modern APIs used here (like
NavigationStackand#Preview) need iOS 16–17. New projects target recent versions by default.
Test yourself
What is the only requirement of the View protocol?
- An
init() - A
bodyproperty - A
render()method - An
@Stateproperty
What does 'declarative' mean in SwiftUI?
- You declare variables with
let - You describe what the UI should look like for the current state, and the framework updates it
- You must declare every pixel position
- Views are declared in XML
Which attribute marks the entry point of a SwiftUI app?
- @main
- @State
- @Preview
- @entry