Search code examples
swiftuiios14xcode12

Selected list row's background remains grey (selected) after navigating back in List (SwiftUI). iOS14 + Xcode12


The selected row remains grey after navigating back from the detail view. Happening both on simulator and real device, only on iOS 14. Does anyone know how to remove it so it behaves the same as on iOS 13 (doesn't remain selected)? This is the only code in the project. (No other navigation's or anything).

let items = ["item1", "item2"]

struct ContentView: View {
    var body: some View {
        NavigationView {
            VStack {
                Text("Hello")
                
                List(items, id: \.self) { item in
                    NavigationLink(destination: Text(item)) {
                        Text(item)
                    }
                }
                .listStyle(PlainListStyle())
            }
        }
//        .navigationViewStyle(StackNavigationViewStyle()) // didn't solve the problem
    }
}

This is how it looks


Solution

  • This is the trick. Just create a ZStack and put an empty button above.

    var body: some View {
        List {
            ForEach(data, id: \.self) { item in
                ZStack {
                    Button("") {}
                    NavigationLink(destination: ItemView(item: item)) {
                        ItemRow(item: item)
                    }
                }
            }
        }
    }