Building a UI with SwiftUI

Harry · 16 Sep 2026 · 2 views
Log in to track your progress and mark lessons complete.

Views describe the screen

In SwiftUI you describe what the UI should look like for the current data, and the framework figures out the rest. A view is a struct conforming to View:

import SwiftUI

struct GreetingView: View {
    var body: some View {
        VStack {
            Text("Hello, GroovyGrails!")
                .font(.title)
            Image(systemName: "star.fill")
                .foregroundColor(.yellow)
        }
        .padding()
    }
}

State drives the UI

Mark changing data with @State. When it changes, SwiftUI re-renders only what needs updating – the UI is a function of state.

SwiftUI: @State is the source of truth; changing it re-renders the view

struct CounterView: View {
    @State private var count = 0
    var body: some View {
        VStack {
            Text("Count: (count)")
            Button("Increment") { count += 1 }
        }
    }
}

Layout with stacks

Compose layouts with VStack (vertical), HStack (horizontal) and ZStack (layered), plus modifiers like .padding() and .background().

Key points

  • A SwiftUI view describes the UI for the current state.
  • @State holds changing data; updating it re-renders the view.
  • Build layouts with VStack/HStack/ZStack and modifiers.
  • You rarely touch pixels directly – you change state.
Share this post:

Comments (0)

Please login or register to comment.

Create a free account to keep reading

You've enjoyed a free tutorial! Register (it's free) to unlock every tutorial, track your progress and save code.

Already have an account? Log in