Jorn Rigter
Jorn Rigter

Reputation: 1195

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

Upvotes: 1

Views: 1661

Answers (3)

joel.d
joel.d

Reputation: 1631

Yea if you try using .previewDisplayName in a #Preview macros nowadays it shows a warning - 1. PreviewDisplayName is ignored in a #Preview macro. Provide the name to the macro initializer, e.g. #Preview("preview name")

You should just use "#Preview("your preview name")"

Upvotes: 0

D'Fontalland
D'Fontalland

Reputation: 345

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

Upvotes: 5

Jorn Rigter
Jorn Rigter

Reputation: 1195

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

Upvotes: 1

Related Questions