从详细视图返回后,所选行仍为灰色。只能在iOS 14上同时在模拟器和真实设备上发生。有人知道如何删除它,使其行为与在iOS 13上相同(不会保持选中状态)吗? 这是项目中的唯一代码。 (没有其他导航或任何导航)。
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
}
}
答案 0 :(得分:5)
这就是窍门。只需创建一个 ZStack 并在上方放置一个空按钮即可。
var body: some View {
List {
ForEach(data, id: \.self) { item in
ZStack {
Button("") {}
NavigationLink(destination: ItemView(item: item)) {
ItemRow(item: item)
}
}
}
}
}
答案 1 :(得分:1)
使用StackNavigationViewStyle()
后,我遇到了同样的问题。
该问题通过使用DefaultNavigationViewStyle()
为我自己解决。
答案 2 :(得分:0)
我实际上找到了lazystack的解决方法,然后将行/单元格与一些填充和分隔符一起放入了Hstack中。但是性能远不如列表。
修改 如果您将列表的外部放置在另一个视图的外部(在您的示例中不要将其嵌入到VStack中),它将可以正常工作。因此,您必须将列表放在单独的View-Struct中,并将其嵌入到ContentView中。像这样:
struct ExtractedListView: View {
var listItems:[something]
List(listItems, id: \.self) { item in
NavigationLink(destination: Text(item)) {
Text(item)
}
}
struct ContentView : View {
NavigationView {
VStack {
Text("Hello")
ExtractedListView(listItems: items)
}
}
}
答案 3 :(得分:0)
这是我使其工作的方式。我发现一些极少数情况下,高光消失会有些滞后,但到目前为止,它起到了很大作用。 本示例使用MVVM模式,希望您能理解。
您可以使用以下第三方库访问SwiftUI列表后面的基础UITableView:
https://github.com/siteline/SwiftUI-Introspect
,然后您可以存储tableView实例,并在表出现和消失时刷新选择(以某种方式在两个表中都做得更好,因为根据用户导航的积极程度,这些块可能会有点滞后地执行)
List {
// ...
}
.introspectTableView { tableView in
self.viewModel.bindToTableView(tableView)
}
.onAppear(perform: {
self.viewModel.clearTableViewSelection()
})
.onDisappear(perform: {
self.viewModel.clearTableViewSelection()
})
在viewModel中,这是函数
func bindToTableView(_ tableView: UITableView) {
self.tableView = tableView
}
func clearTableViewSelection() {
// This is a iOS14 hack that prevents clicked cell background view to remain highlighted when we come back to the screen
if #available(iOS 14, *){
DispatchQueue.main.async {
if let selectedIndexPath = self.tableView?.indexPathForSelectedRow {
self.tableView?.deselectRow(at: selectedIndexPath, animated: false)
if let selectedCell = self.tableView?.cellForRow(at: selectedIndexPath) {
selectedCell.setSelected(false, animated: false)
}
}
}
}
}
答案 4 :(得分:0)
您可以像这样包装视图,这特别有用,尤其是在使用列表等的情况下。此解决方案也消除了插入符号。
ZStack {
NavigationLink(destination: DestinationView()) {
EmptyView()
}
Rectangle().background(Color(UIColor.systemBackground)).foregroundColor(Color(UIColor.systemBackground))
ViewToClick()
}
答案 5 :(得分:0)
这对我有用:
UPDATE_DISCONNECTED