Created
October 31, 2019 19:56
-
-
Save perlguy99/a0cbc3e5cc336b114eb78404d0c667da to your computer and use it in GitHub Desktop.
SwiftUI Showing a Sheet
This file contains hidden or 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
// 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