Skip to content

Instantly share code, notes, and snippets.

@trozware
Created October 4, 2019 07:13
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save trozware/b6729aeb8a14b11d66df33f198a3641a to your computer and use it in GitHub Desktop.
Save trozware/b6729aeb8a14b11d66df33f198a3641a to your computer and use it in GitHub Desktop.
Demonstration of dismissing sheet in SwiftUI and what can trigger the "onDismiss" action.
import SwiftUI
struct ContentView: View {
@State private var showingSheet = false
@State private var counter = 0
var body: some View {
VStack(spacing: 50.0) {
Text("Sheet has been shown \(counter) times.")
Text("The counter will be incremented whenever the sheet's onDismiss closure is triggered.")
.lineLimit(3)
.multilineTextAlignment(.center)
Button("Show Sheet") {
self.showingSheet = true
}
}
.padding()
.sheet(isPresented: $showingSheet, onDismiss: {
self.counter += 1
}) {
SheetView(isPresented: self.$showingSheet)
}
}
}
struct ContentView_Previews: PreviewProvider {
static var previews: some View {
ContentView()
}
}
struct SheetView: View {
@Environment(\.presentationMode) var presentationMode
@Binding var isPresented: Bool
var body: some View {
VStack(spacing: 50.0) {
Text("Here is the sheet view.")
Button("Dismiss Sheet with Environment") {
self.presentationMode.wrappedValue.dismiss()
}
Button("Dismiss Sheet with Binding") {
self.isPresented = false
}
Text("Pull down to dismiss AND trigger 'onDismiss' action.")
.lineLimit(3)
.multilineTextAlignment(.center)
}
.padding()
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment