When adding a NavigationLink in SwiftUI the destination is presented twice, ex: I tap on the NavigationLink and it pushes my destination but when I dismiss the destination, via the back button or the swipe gesture it pushes the destination again without taping on the link. Here is the part of my code that handles the link:
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) .onLongPressGesture { print("looong") self.managedObjectContext.delete(imageRef) do { try self.managedObjectContext.save() } catch { print("error deleting: \(error.localizedDescription)") } } ZStack(alignment: .center) { Circle() .foregroundColor(Color.appColors.lightRose) .opacity(0.7) .frame(width: 35, height: 35) Image(systemName: "arkit") .imageScale(.large) } NavigationLink(destination: ZStack { Color.appColors.rose .edgesIgnoringSafeArea(.top) ReferenceARSwiftUIView(currentImage: imageRef.getUIImage()) .navigationBarTitle("AR Reference") } ) { EmptyView() .frame(width: 90, height: 90) } } }.animation(.interpolatingSpring(stiffness: 0.5, damping: 0.5)) EDIT 01: As suggested I removed a bit of the noise in the code:
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())) { EmptyView() .frame(width: 90, height: 90) } } }.animation(.interpolatingSpring(stiffness: 0.5, damping: 0.5)) EDIT 02: I think I narrowed down, basically if I remove the ForEach the NavigationLink pushes correctly to the next View. Also depending on the number of itens I have on my array for the ForEach the number of pushes is the same.
Color.appColors.lightRose,Image, unneeded stacks) that really add noise. Maybe if you strip it down toNavigationView,NavigationLink, and maybe theForEachto have us duplicate your issue, you might find it's working... and at that point you can add back in the rest to see why the code you posted isn't.ForEachwithout aList. Would this help? hackingwithswift.com/quick-start/swiftui/… More, (2) I actively check this site for the SwiftUI tag. I recall several questions with regards to bothListndNavigationLink(evenButton) behavior in loops. Have you searched this site for those? Truth is, I stopped using the combination ofNavigationViewandListbecause of how... raw... SwiftUI is compared to UIKit,ScrollViewas suggested below. I am only able to get it working if I don't use ForEach.