SwiftUI Complex Snippets Cheatsheet
This page restructures the original SwiftUI snippets into a readable Markdown cheatsheet. All original comments and code are preserved exactly; only headings and fenced code blocks were added.
Perform an action on return from background
// Perform an action on return from background
struct ContentView: View {
@Environment(\.scenePhase) private var scenePhase
var body: some View {
Text("Hello, world!")
.padding()
.onAppear {
// Perform tasks that should run when the view appears
performTask()
}
.onChange(of: scenePhase) { newPhase in
if newPhase == .active {
// Perform tasks that should run when the app comes back from background
performTask()
}
}
}
private func performTask() {
// Your task goes here
print("Task performed")
}
}
Sheets
// Sheets
struct ContentView: View {
@Environment(\.scenePhase) private var scenePhase
var body: some View {
VStack {
Text("Hello, world!")
}
.sheet(isPresented: $isShowingEndTimeSheet) {
SettingsDateSheetView(
handler: updateEndTime,
initHours: envModel.endTimeHours,
initMinutes: envModel.endTimeMinutes,
hoursToCompare: envModel.startTimeHours,
minutesToCompare: envModel.startTimeMinutes,
isCurrentLater: true
)
// to turn off interactions with the sheet
.interactiveDismissDisabled(true)
// to limit the size
.presentationDetents([.height(350)])
}
}
}
Nested TabView
// Nested TabView
// inner TabView
TabView(selection: $viewModel.selectedTab) {
// pages
}
// remove tabBar for inner
.tabViewStyle(PageTabViewStyle(indexDisplayMode: .never))
Back from background mode
// Back from background mode
struct SomeView: View {
@Environment(\.scenePhase) private var scenePhase
var body: some View {
VStack {
Text("Text")
}
.onChange(of: scenePhase) { newPhase in
if newPhase == .active {
Task {
DispatchQueue.main.async {
// Do something async
}
}
}
}
}
}
Foreground/background via notifications
// Another way of taking app from background or foreground
struct SomeView: View {
var body: some View {
VStack {
Text("Text")
}
.onReceive(NotificationCenter.default.publisher(for: UIApplication.willResignActiveNotification)) { _ in
// app is going to the background, so do something
}
.onReceive(NotificationCenter.default.publisher(for: UIApplication.willEnterForegroundNotification)) { _ in
// app is coming to the foreground, so do something else
}
}
}
Lifecycle modifiers quick reference
// works ONLY when returning from background
.onReceive(NotificationCenter.default.publisher(for: UIApplication.willEnterForegroundNotification))
// works every time a view appears on the screen — becomes active
.onChange(of: scenePhase)
// handle async operations only on Mount
.task
// same as task but sync operations
.onAppear
Track swipe gestures
// Track swipe gestures
struct ContentView: View {
var body: some View {
VStack {
Text("Text")
}
.gesture(DragGesture(minimumDistance: 3.0, coordinateSpace: .local)
.onEnded { value in
print(value.translation)
switch(value.translation.width, value.translation.height) {
case (...0, -30...30): print("left swipe")
case (0..., -30...30): print("right swipe")
case (-100...100, ...0): print("up swipe")
case (-100...100, 0...): print("down swipe")
default: print("no clue")
}
}
)
}
}