skip to Main Content

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

Answers


  1. Chosen as BEST ANSWER

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

  2. 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)
    }
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search