Showing multiple previews in Xcode 15 and up using Preview Macro

640 views Asked by At

How can I show two or more different variants in Xcode using the new #Preview macro?

Without the macro, this was possible:

struct TutorialView_Previews: PreviewProvider {
    static var previews: some View {
        MyAwesomeView(title: "Title One", hideSomePart: true)
            .previewDisplayName("Hidden some part")
        
        MyAwesomeView(title: "Title Two", hideSomePart: false)
            .previewDisplayName("Showing all parts")
    }
}

But with the new macro, this isn't compiling:

#Preview {
    MyAwesomeView(title: "Title One", hideSomePart: true)
        .previewDisplayName("Hidden some part")
        
    MyAwesomeView(title: "Title Two", hideSomePart: false)
        .previewDisplayName("Showing all parts")
}
2

There are 2 answers

2
D'Fontalland On BEST ANSWER

Using .previewDisplayName("...") does not work for me. I got it working doing it like this:

#Preview("Hidden some part") {
  MyAwesomeView(title: "Title One", hideSomePart: true)
}

#Preview("Showing all parts") {   
  MyAwesomeView(title: "Title Two", hideSomePart: false)
}
3
Jorn Rigter On

Turned out to be a simple fix:

#Preview {
    MyAwesomeView(title: "Title One", hideSomePart: true)
        .previewDisplayName("Hidden some part")
}

#Preview {   
    MyAwesomeView(title: "Title Two", hideSomePart: false)
        .previewDisplayName("Showing all parts")
}