NavigationLink on SwiftUI pushes view twice

I'm sure that Part conforms to Identifiable protocol and has id property.

The destination of the NavigationLink is presented multiple times because you have multiple instances of Part that have the exact same identity id in the forEach loop.

Just make sure that each instance of Part has a unique id and everything will work as expected.


I solved this by setting the NavigationLink's tag to the unique id of the model item.

Assuming you have an id for your imageRef, ReferenceImage class.

var part: Part
var body: some View {
    HStack(spacing: 8.0) {
        ForEach(part.getReference()) { (imageRef: ReferenceImage) in
            ZStack(alignment: .topTrailing) {
                Image(uiImage: imageRef.getUIImage())
                .resizable()
                .frame(width: 90, height: 90)
                .cornerRadius(6)
                NavigationLink(destination: ReferenceARSwiftUIView(currentImage: imageRef.getUIImage()), tag: imageRef.id) {
                    EmptyView()
                    .frame(width: 90, height: 90)
                }

            }
        }.animation(.interpolatingSpring(stiffness: 0.5, damping: 0.5))
NavigationLink(destination: ReferenceARSwiftUIView(currentImage: imageRef.getUIImage()), tag: imageRef.id)

Tags:

Ios

Swiftui