TExcelTAD
Home › Swift › SwiftUI Intro

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:

swift
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:

html · try it yourself
<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 the View protocol. Structs are cheap value types, so SwiftUI can create thousands of them.
  • var body: some View — the only requirement of View. It returns the content. some View means "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:

swift · try it yourself
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:

swift
#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 NavigationStack and #Preview) need iOS 16–17. New projects target recent versions by default.

Test yourself

What is the only requirement of the View protocol?

  1. An init()
  2. A body property
  3. A render() method
  4. An @State property

What does 'declarative' mean in SwiftUI?

  1. You declare variables with let
  2. You describe what the UI should look like for the current state, and the framework updates it
  3. You must declare every pixel position
  4. Views are declared in XML

Which attribute marks the entry point of a SwiftUI app?

  1. @main
  2. @State
  3. @Preview
  4. @entry