我有一个 SwiftUI
List
,它应该是 .refreshable()
并且也是 .dropDestination()
。虽然列表中有项目,但我可以将它们放在特定的索引处,如下所示:
List {
ForEach(items) { item in
...
}
.dropDestination(for: URL.self) { items, index in
handleUrlDrop(items, at: index)
}
}
.refreshable {
await loadNewItems()
}
我正在努力解决的是在空的时候让它成为
.dropDestination()
。这是我尝试过的:
ZStack {
List {
...
}
.refreshable {
await loadNewItems()
}
BackgroundPlaceholder()
.dropDestination(for: URL.self) { items, _ in
handleUrlDrop(items)
return true
}
}
它可以工作,但我只能放到
BackgroundPlaceholder
上,并且无法通过滑动来刷新。我也尝试过.allowsHitTesting(false)
,但这使它不再是一个下降目的地。将 Color.white.opacity(0.0001)
之类的东西放在上面也同样如此。
我也许可以将
BackgroundPlaceholder
放在列表中,但这意味着它会滚动,有列表分隔线,我可能必须用 GeometryReader
之类的东西填充整个高度,这看起来很糟糕。有什么想法吗?
您可以根据是否有项目来更改列表显示的内容,而不是添加叠加层,即
List {
if !items.isEmpty {
ForEach(items) { item in
...
}
.dropDestination(for: URL.self) { items, index in
handleUrlDrop(items, at: index)
}
} else {
// put a view that is a drop destination here
}
}
如果您希望整个列表可删除,您可以使用仅显示一行的
ForEach
。 ForEach
使整个列表可删除。
// in the else branch...
ForEach(0..<1) { _ in
// this is a completely invisible row
Color.clear
.listRowBackground(Color.clear)
// alternatively, consider using a ContentUnavailableView:
// ContentUnavailableView("Drop something here!", systemImage: "square.and.arrow.down")
}
.dropDestination(for: URL.self) { items, _ in
handleUrlDrop(items)
}
IMO,使用
ForEach
感觉有点滥用。如果您不需要整个列表都是可删除的,我只需放置一行 ContentUnavailableView
并且只允许将内容拖放到该行上。