SwiftUI Preview Sheet w/o Running Live Preview?

1.7k views Asked by At

Update Xcode 13 The code sample below works as expected in Xcode 13.

Update from Apple Frameworks Engineer October 2020:

Unfortunately there is no current workarounds to let you preview this outside of the live preview.


Is it possible to create a SwiftUI preview of a presented sheet without running the Live Preview? For example:

struct Sheet_Previews: PreviewProvider {
    static var previews: some View {
        Text("Background").sheet(isPresented: .constant(true)) {
            Text("Sheet")
        }
    }
}

The above results in the following preview:

enter image description here

In order for the sheet content to be presented in the preview, you must run the Live Preview:

enter image description here

2

There are 2 answers

2
Florian Mielke On BEST ANSWER

Xcode 13.0 seems to handle this correctly without starting a Live Preview.

So this is working now:

struct Sheet_Previews: PreviewProvider {
    static var previews: some View {
        Text("Background").sheet(isPresented: .constant(true)) {
            Text("Sheet")
        }
    }
}
2
Asperi On

Just let preview provide to show both views, for parent and sheet, like

struct Sheet_Previews: PreviewProvider {
    static var previews: some View {
        Group {       // << will create two previews
            Text("Background")  // << parent view here
            Text("Sheet")       // << sheet view here
        }
    }
}