8

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?

3 Answers 3

16

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) 
    }
}
Sign up to request clarification or add additional context in comments.

3 Comments

I know that, Thats what I am asking what to give as a parameter there.
yikes my fault, didn't read your question carefully. look at my edit
This is actually the more elegant solution. Too bad it takes a second read to get there :)
10

Late Answer but I found one more option below (I came here on SO to search for Namespace)

add Namespace().wrappedValue

example: -

struct TabItemView_Previews: PreviewProvider {
    static var previews: some View {
        TabItemView(tintColor: .blue, inActiveColor: .gray, tab: .home, tabAnimation: Namespace().wrappedValue, activeTab: .constant(.home))
    }
}

Comments

9

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

2 Comments

Yes it is working, Can you please explain a little, what's happening? Thank You.
We just create preview-only wrapper view that provides all needed parameters to actually target view.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.