Multiple NavigationLinks leading to a UIViewControllerRepresentable only works first time

Originator:jeremy.gale
Number:rdar://FB9615402 Date Originated:2021-09-10
Status:Open Resolved:
Product:SwiftUI Framework Product Version:iOS 14.7
Classification: Reproducible:Every time
 
When creating multiple nested `NavigationLink`s where the final destination is a `UIViewControllerRepresentable`, the destination will only be shown on the first try. On subsequent tries, it will load a blank screen.

# System

iPhone 11 Pro Simulator, 14.5, Xcode 12.5 (12E262)
Also: iPhone 11 Pro running iOS 14.7.1

# Steps to Reproduce

* Use the following code in a basic SwiftUI app:

```swift
class HelloWorldVC: UIViewController {
    override func loadView() {
        super.loadView()
        let label = UILabel()
        label.text = "Hello, World"
        view.addSubview(label)

        label.translatesAutoresizingMaskIntoConstraints = false
        label.trailingAnchor.constraint(equalTo: view.safeAreaLayoutGuide.trailingAnchor).isActive = true
        label.leadingAnchor.constraint(equalTo: view.safeAreaLayoutGuide.leadingAnchor).isActive = true
        label.topAnchor.constraint(equalTo: view.safeAreaLayoutGuide.topAnchor).isActive = true
        label.bottomAnchor.constraint(equalTo: view.safeAreaLayoutGuide.bottomAnchor).isActive = true
    }
}

struct ViewControllerContainer: UIViewControllerRepresentable {
    let vc: UIViewController

    func makeUIViewController(context: Context) -> some UIViewController { vc }
    func updateUIViewController(_ uiViewController: UIViewControllerType, context: Context) {}
}

struct ContentView: View {
    var body: some View {
        NavigationView {

            // Works:
            //NavigationLink("View UIKit VC", destination: ViewControllerContainer(vc: HelloWorldVC()))

            // Only loads the UIKit view once. It's blank on subsequent tries.
            NavigationLink(
                "Detail Screen",
                destination: NavigationLink(
                    "View UIKit VC",
                    destination: ViewControllerContainer(vc: HelloWorldVC())
                )
            )
        }
    }
}
```

* Tap "Detail Screen"
* Tap "View UIKit VC". You will see the "Hello, World" UIViewController.
* Tap Back
* Tap "View UIKit VC"

Expected: 
* You should see the "Hello, World" UIViewController again

Actual:
* You will see a blank view. This will happen as many times as you try.

This is unexpected and will cause accessibility users confusion.

Screenshot:

![Video of bug](https://github.com/jgale/NavigationLinkUIVCRBug/blob/main/navigation_link_bug.mov)

Comments

I realize that I am not using UIViewControllerRepresentable correctly. It needs to create a new UIVieController inside makeUIViewController. Doing so fixes my problem. Closing this bug.

By jeremy.gale at Sept. 13, 2021, 3:12 p.m. (reply...)

Please note: Reports posted here will not necessarily be seen by Apple. All problems should be submitted at bugreport.apple.com before they are posted here. Please only post information for Radars that you have filed yourself, and please do not include Apple confidential information in your posts. Thank you!