Created
October 4, 2019 07:13
-
-
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.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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