Skip to content

Instantly share code, notes, and snippets.

@sanzaru
Last active May 9, 2022 23:32
Show Gist options
  • Save sanzaru/c16b108c3281859f581e0efd21f6bc47 to your computer and use it in GitHub Desktop.
Save sanzaru/c16b108c3281859f581e0efd21f6bc47 to your computer and use it in GitHub Desktop.
Example for demonstrating the delay on setting the bound variable to nil, when sheet is dismissed by drag
import SwiftUI
struct ContentView: View {
private enum ActiveSheet: Identifiable {
case first, second
var id: Int { hashValue }
}
@State private var activeSheet: ActiveSheet?
var body: some View {
VStack(spacing: 30) {
if activeSheet == nil {
Button("First sheet") { activeSheet = .first }
Button("Second sheet") { activeSheet = .second }
} else {
Text("Sheet still open...")
}
}
.frame(maxWidth: .infinity, maxHeight: .infinity)
.sheet(item: $activeSheet, onDismiss: dismiss) { sheet in
switch sheet {
case .first:
VStack(spacing: 30) {
Text("First sheet")
Button("Close", action: dismiss)
}
.frame(maxWidth: .infinity, maxHeight: .infinity)
.background(Color.blue.opacity(0.3))
case .second:
VStack(spacing: 30) {
Text("Second sheet")
Button("Close", action: dismiss)
}
.frame(maxWidth: .infinity, maxHeight: .infinity)
.background(Color.green.opacity(0.3))
}
}
}
func dismiss() {
activeSheet = nil
}
}
struct ContentView_Previews: PreviewProvider {
static var previews: some View {
ContentView()
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment