Post

Replies

Boosts

Views

Activity

Reply to SwiftUI NavigationLink in List on iOS 14
Yes sometime cells stay highlighted for some reason. For example I found that this problem could appear if you add a padding and a background color to a list. This list for example should have the problem and the rows will stay highlighted after back is pressed. struct TestListView: View { 		var body: some View { 				NavigationView { 						List { 								NavigationLink( 										destination: Text("Detail 1"), 										label: { 												Text("Show detail 1") 										} 								) 								NavigationLink( 										destination: Text("Detail 2"), 										label: { 												Text("Show detail 2") 										} 								) 						} 						.padding(.top, 10) 						.background(Color.blue) 				} 		} } You can try to remove some of the list modifiers. If you still have the problem you can try with Introspect. https://github.com/siteline/SwiftUI-Introspect The above code could be fixed in this way: struct TestListView: View { 		 		@State private var tableView: UITableView? 		private func deselectRows() { 				if let tableView = tableView, let selectedRow = tableView.indexPathForSelectedRow { 						tableView.deselectRow(at: selectedRow, animated: true) 				} 		} 		 		var body: some View { 				NavigationView { 						List { 								NavigationLink( 										destination: Text("Detail 1").onAppear { 												deselectRows() 										}, 										label: { 												Text("Show detail 1") 										} 								) 								NavigationLink( 										destination: Text("Detail 2").onAppear { 												deselectRows() 										}, 										label: { 												Text("Show detail 2") 										} 								) 						} 						.padding(.top, 10) 						.background(Color.blue) 						.introspectTableView(customize: { tableView in 								self.tableView = tableView 						}) 				} 		} } A little bit ugly but it works.
Topic: UI Frameworks SubTopic: SwiftUI Tags:
Oct ’20