Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

NavigationLink on SwiftUI pushes view twice

Tags:

ios

swiftui

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.

like image 651
Matheus Tusi Avatar asked Sep 15 '19 16:09

Matheus Tusi


People also ask

How does NavigationLink work SwiftUI?

NavigationLink in SwiftUI allows pushing a new destination view on a navigation controller. You can use NavigationLink in a list or decide to push a view programmatically. The latter enables you to trigger a new screen from a different location in your view.

How do I pop to a specific view in SwiftUI?

ContentView -> View1 -> View2 And from View2 you want to pop to the Root view.

How do I disable the navigation back button in SwiftUI?

The . navigationBarBackButtonHidden(true) will hide the back button.


2 Answers

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.

like image 184
Mohammed Avatar answered Sep 29 '22 14:09

Mohammed


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)
like image 43
Tom GODDARD Avatar answered Sep 29 '22 14:09

Tom GODDARD