xcodepreviewxcode15

Showing multiple previews in Xcode 15 and up using Preview Macro


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")
}

Solution

  • 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)
    }