I am creating a subview in SwiftUI with few parameters. One of the parameters' type is Namspace.ID. So I have to pass the Namespace.ID type parameter through preview to see the view in Xcode canvas. I don't know how to do it. My codes look like this.
struct BagView: View {
var bagData:BagModel
var animation:Namespace.ID
var body: some View {
VStack{
ZStack{
Color(bagData.image)
.cornerRadius(15)
Image(bagData.image)
.resizable()
.aspectRatio(contentMode: .fit)
.padding(20)
.matchedGeometryEffect(id: bagData.image, in: animation)
}
Text(bagData.title)
.fontWeight(.heavy)
.foregroundColor(.gray)
Text(bagData.price)
.fontWeight(.heavy)
.foregroundColor(.black)
}
}
}
struct BagView_Previews: PreviewProvider {
static var previews: some View {
BagView(bagData: BagModel(image: "bag1", title: "The Bag", price: "$123"))
// I am getting an error here: Missing argument for parameter 'animation' in call
}
}
How can I see the view in Canvas by resolving the error?
The error tells you, that you forgot a parameter, since animation is a required and non-optional field. So you need to add a value for the property "animation". Something like that:
BagView(bagData: BagModel(...), animation: <valueHere>)
EDIT: You should be able to inject a Namespace value doing the following:
struct BagView_Previews: PreviewProvider {
@Namespace static var namespace // <- This
static var previews: some View {
BagView(bagData: BagModel(image: "bag1", title: "The Bag", price: "$123"), animation: namespace)
}
}
Here is possible approach - use intermediate test/preview view:
struct BagView_Previews: PreviewProvider {
struct TestView: View {
@Namespace var ns
var body: some View {
BagView(bagData: BagModel(image: "bag1", title: "The Bag", price: "$123"),
animation: ns)
}
}
static var previews: some View {
TestView()
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With