Updated for Xcode 12.0
SwiftUI’s sheets are used to present new view controllers modally over existing ones, like calling present()
on a UIViewController
in UIKit. To use one, give it something to show (some text, an image, a custom view, etc), add a Boolean that defines whether the detail view should be showing, then attach it to your main view as a modal sheet.
For example, if you had a detail view like this one:
struct DetailView: View {
var body: some View {
Text("Detail")
}
}
Then you could present it like this:
struct ContentView: View {
@State var showingDetail = false
var body: some View {
Button(action: {
self.showingDetail.toggle()
}) {
Text("Show Detail")
}.sheet(isPresented: $showingDetail) {
DetailView()
}
}
}
Unlike navigation links, sheets don’t require a navigation view to work.
SPONSORED Would you describe yourself as knowledgeable, but struggling when you have to come up with your own code? Fernando Olivares has a new book containing iOS rules you can immediately apply to your coding habits to see dramatic improvements, while also teaching applied programming fundamentals seen in refactored code from published apps.
Sponsor Hacking with Swift and reach the world's largest Swift community!
Link copied to your pasteboard.