NavigationLink stays highlighted after returning from Detail View

i have an existing UINavigationController and i am pushing a UIHostingController with a SwiftUI View that contains a List as its top-level element.

The List contains several rows created using NavigationLink where tapping a row navigates the user to a detail view.

Upon returning, however, the selected row remains highlighted.

The only workaround i have found is the rather ugly piece of code shown below:

Code Block swift
List {
ZStack {
Button(action: {}) {
Text("Detail")
}
NavigationLink(destination: DetailView()) {
EmptyView()
}
}
...
}

This feel ridiculous and borderline insane to have to do for each row in the List.

is there a better way or are SwiftUI Lists just broken in iOS 14?

Yes, I have the same problem in some lists in my app. Rows stay highlighted after navigating back.

I have created a really easy solution to remove the highlight color in these situations. I use NotificationCenter to notify when the NavigationLink is tapped and use some UIKit-magic to deselect the cells. Works like a charm. All you need to do is add this block of code. Note that if your list has multiple sections, you have to modify the code.

Code Block
.onReceive(NotificationCenter.default.publisher(for: UITableView.selectionDidChangeNotification)) { notification in
            if let tableView = notification.object as? UITableView {
                let numberOfRows = tableView.numberOfRows(inSection: 0)
                guard numberOfRows > 0 else { return }
                for i in 0...rows - 1 {
                    tableView.deselectRow(at: IndexPath(row: i, section: 0), animated: true)
                }
            }
        }


NavigationLink stays highlighted after returning from Detail View
 
 
Q