Lesson 6 โ€ข Intermediate

    SwiftUI Basics ๐Ÿ“ฑ

    Build your first iOS screen using Apple's declarative UI framework. Learn views, modifiers, layout stacks, and state management to create beautiful, reactive interfaces.

    What You'll Learn in This Lesson

    • โ€ข Core SwiftUI views: Text, Image, Button, TextField, Toggle
    • โ€ข View modifiers and chaining
    • โ€ข Layout with VStack, HStack, ZStack, and Spacer
    • โ€ข State management: @State, @Binding, @StateObject
    • โ€ข NavigationStack and List for building real app screens

    1๏ธโƒฃ Views and Modifiers

    Everything in SwiftUI is a View. Views are lightweight structs that describe what to display. You customize them by chaining modifiers like .font(), .padding(), and .foregroundColor().

    Try It: Views & Modifiers

    Explore core SwiftUI views and modifier chaining

    Try it Yourself ยป
    JavaScript
    // SwiftUI โ€” Declarative UI Framework
    console.log("=== What is SwiftUI? ===");
    console.log("SwiftUI is Apple's declarative UI framework (introduced 2019).");
    console.log("Instead of TELLING the UI what to do step by step,");
    console.log("you DESCRIBE what the UI should look like.");
    console.log();
    
    // Simulating SwiftUI view hierarchy
    console.log("=== Your First SwiftUI View ===");
    console.log();
    console.log("struct ContentView: View {");
    console.log("    var body: some View {");
    console.log('  
    ...

    2๏ธโƒฃ Layout with Stacks

    SwiftUI uses three stack types for layout: VStack (vertical), HStack (horizontal), and ZStack (layered). Combined with Spacer() and padding(), you can build any layout without constraints.

    Try It: Layout Stacks

    VStack, HStack, ZStack, and building a complete profile screen

    Try it Yourself ยป
    JavaScript
    // SwiftUI Layout โ€” Stacks, Spacing, and Alignment
    console.log("=== Layout Containers ===");
    console.log();
    
    // VStack โ€” vertical
    console.log("๐Ÿ“ VStack (vertical stack):");
    console.log("  VStack(alignment: .leading, spacing: 10) {");
    console.log('      Text("Title")');
    console.log('      Text("Subtitle")');
    console.log('      Text("Body text")');
    console.log("  }");
    console.log("  โ†’ Items stacked top to bottom");
    console.log();
    
    // HStack โ€” horizontal
    console.log("๐Ÿ“ HStack (horizontal stack):"
    ...

    3๏ธโƒฃ State Management

    SwiftUI is reactive โ€” when state changes, the UI automatically updates. @State is for local view state, @Binding shares state between parent and child, and @StateObject/@ObservedObject manage external data models.

    Try It: State Management

    @State, @Binding, @StateObject, and NavigationStack

    Try it Yourself ยป
    JavaScript
    // SwiftUI State Management
    console.log("=== @State โ€” Local View State ===");
    console.log();
    
    // Simulating @State
    console.log("struct CounterView: View {");
    console.log("    @State private var count = 0");
    console.log();
    console.log("    var body: some View {");
    console.log("        VStack(spacing: 20) {");
    console.log('            Text("Count: \\(count)")');
    console.log("                .font(.largeTitle)");
    console.log();
    console.log('            Button("Increment") {');
    console.log("        
    ...

    โš ๏ธ Common Mistakes

    โš ๏ธ
    Using @ObservedObject instead of @StateObject โ€” @StateObject creates and owns the object; @ObservedObject only observes it. Using the wrong one causes data loss on re-renders.
    โš ๏ธ
    Modifier order matters โ€” .background(.blue).padding() vs .padding().background(.blue) produce very different results.
    ๐Ÿ’ก
    Pro Tip: Use Xcode's Canvas preview (โŒ˜+Option+P) to see live changes as you code โ€” no need to build and run.

    ๐Ÿ“‹ Quick Reference โ€” SwiftUI

    ComponentUsage
    TextText("Hello").font(.title)
    ButtonButton("Tap") { action() }
    VStackVStack { Text("A"); Text("B") }
    @State@State private var count = 0
    @Binding@Binding var isOn: Bool
    NavigationStackNavigationStack { List { ... } }

    ๐ŸŽ‰ Lesson Complete!

    You've learned the foundations of SwiftUI! Next, learn how to fetch data from APIs and display it in your app.

    Sign up for free to track which lessons you've completed and get learning reminders.

    Previous

    Cookie & Privacy Settings

    We use cookies to improve your experience, analyze traffic, and show personalized ads. You can manage your preferences below.

    By clicking "Accept All", you consent to our use of cookies for analytics and personalized advertising. You can customize your preferences or reject non-essential cookies.

    Privacy Policy โ€ข Terms of Service