Skip to content

Instantly share code, notes, and snippets.

@perlguy99
Created October 31, 2019 19:56
Show Gist options
  • Save perlguy99/a0cbc3e5cc336b114eb78404d0c667da to your computer and use it in GitHub Desktop.
Save perlguy99/a0cbc3e5cc336b114eb78404d0c667da to your computer and use it in GitHub Desktop.
SwiftUI Showing a Sheet
// Have a View that we want to show in the sheet
struct MyView: View {
var body: some View {
Text("This is my view")
}
}
struct ContentView: View {
// Need something to track the state of the sheet
@State private var showingSheet = false
var body: some View {
Button("Show Sheet") {
// show or hide the sheet
self.showingSheet.toggle()
}
// .sheet is a modifier (like alert)
// Add it in the view hierarchy somewhere
.sheet(isPresented: $showingSheet) {
SecondView()
}
}
}
// If you wanted to pass data to the sheet, it would look like this instead
struct ContentView: View {
@State private var showingSheet = false
var body: some View {
Button("Show Sheet") {
self.showingSheet.toggle()
}
.sheet(isPresented: $showingSheet) {
SecondView(name: "Brent")
}
}
}
struct SecondView: View {
var name: String
var body: some View {
Text("Hello, \(name)")
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment