Back button in UIViewControllerRepresentable goes to root view instead of going back to previous view

422 Views Asked by At

I have a SwiftUI app that has three levels:

  • Main List (SwiftUI NavigationView)
  • Secondary List (SwiftUI NavigationView)
  • An UIViewControllerRepresentable

In my secondary list I have

NavigationLink(destination: ARView(museum: self.museum)) {
    Image(systemName: "camera.viewfinder")
})

And then in the UIViewControllerRepresentable I have

struct ARView: UIViewControllerRepresentable {

var museum:Museum?
var work:Work?

func makeUIViewController(context: UIViewControllerRepresentableContext<ARView>) -> UIViewController {
    let ARVC = ARViewController(nibName: "ARViewController", bundle: .main)
    if let museum = museum {
        ARVC.visitedMuseum = museum
        ARVC.ARExperience = false
        LoggingSystem.push(eventLog: ["event":"AR opened","museum":ARVC.visitedMuseum.name], verbose: false)
    }
    if let work = work {
        ARVC.visitedWork = work
        ARVC.VRonly = true
        LoggingSystem.push(eventLog: ["event":"Work selected","work":ARVC.visitedWork.name], verbose: false)
    }
    return ARVC
}

func updateUIViewController(_ uiViewController: UIViewController, context: UIViewControllerRepresentableContext<ARView>) {
    
}
}

The back button that appears should go back to the Secondary List, but instead it goes back to the Main List.

1

There are 1 best solutions below

0
On

I solved my problem! The NavigationLink to my UIViewControllerRepresentable was in the NavigationBar and that messed up with the hierarchy.

I moved the NavigationLink outside of the NavigationBar and now is working as expected.