SwiftUI Basic
State with ObservableObject
Use ObservableObject to hold shared state, and @StateObject in the creating view to own its lifecycle.
Views update automatically when @Published properties change.
Syntax: class VM: ObservableObject { @Published var count }, @StateObject private var vm = VM(), bind UI to vm.count.
This example refactors the basic counter above.
The UI and behavior are the same (a counter label and button), but the state now lives in a CounterModel view model managed by @StateObject. This pattern scales better as your app grows.
Stacks and Spacing
Use VStack, HStack, and ZStack to arrange views vertically, horizontally, or in layers.
Spacing and alignment are configurable.
Syntax:
VStack(alignment: .leading, spacing: 8) { ... }HStack { Text("A"); Spacer(); Text("B") }ZStack { Color.blue; Text("Overlay") }
Overlays and Alignment
Layer content with ZStack and control placement using alignment and alignmentGuide.
Get Rajabhishek’s stories in your inbox
Join Medium for free to get updates from this writer.
Syntax:
ZStack(alignment: .topLeading) { ... }.alignmentGuide(.top) { d in d[.bottom] }
SwiftUI Layout: GeometryReader
Read parent size and position to build responsive layouts with GeometryReader.
GeometryReader
Use GeometryReader to read parent size and position to build responsive layouts.
SwiftUI Layout: Lazy Stacks
Use LazyVStack/LazyHStack to efficiently render large lists of views on demand.
Lazy Stacks
Lazy Stacks are used to efficiently render large lists of views on demand.
Syntax:
LazyVStack { ForEach(...) { ... } }LazyHStack { ForEach(...) { ... } }
SwiftUI Navigation
Learn to navigate with NavigationStack, push screens with NavigationLink, and present sheets and alerts.
NavigationStack and NavigationLink
Use NavigationStack with NavigationLink to push views onto a stack.
Add titles and toolbar items.
Syntax:
NavigationStack { List { NavigationLink("Title", destination: Detail()) } }.navigationTitle("Title").toolbar { ... }





