Home > Software engineering >  How can you preview a swift view that uses ViewBuilder
How can you preview a swift view that uses ViewBuilder

Time:05-11

How can I pass test content to preview in this example. I get the error "Generic parameter 'Content' could not be inferred" and I don't understand what that is saying.

struct CoolView<Content:View>: View {
    
    let content: Content
    
    init(@ViewBuilder content: () -> Content) {
        self.content = content()
    }
    
    var body: some View {
        ZStack(alignment: .bottom) {
            Color.green
                .cornerRadius(40)
            
            VStack() {
                Spacer()
                content
                Spacer()
            }
            
        }
    }
}


struct CoolView_Previews: PreviewProvider {
    static var previews: some View {
        CoolView()
    }
}

CodePudding user response:

Something like below. You need to return a view for preview to compile.

struct CoolView_Previews: PreviewProvider {
    static var previews: some View {
      CoolView {
        Text("Testing")
      }
    }
}
  • Related