Nic*_*lli 7 list ios swift swiftui
我在 SwiftUI 中有一个列表,如下所示:
@State var items = ["item0", "item1", "item2"]
List {
ForEach(items, id: \.self) { item in
ZStack {
Rectangle()
.frame(width: 150, height: 65)
Text(item)
}
.onMove { from, to in
items.move(fromOffsets: from, toOffset: to)
}
}
.listRowSeparator(.hidden)
.listRowBackground(Color.clear)
}
.listStyle(.plain)
.scrollContentBackground(.hidden)
Run Code Online (Sandbox Code Playgroud)
这个想法是在 ForEach 中显示项目,就好像它们只是“浮动”在屏幕上,而不是在列表中。
但是,当我拖动列表项(以在列表中重新排序;从 .onMove() 修饰符)时,会出现白色背景:
我想删除这个背景,所以看起来我只是移动列表项(只是圆角矩形,没有背景)。我怎样才能实现这个目标?
编辑:我添加了.onDrag {} \@ViewBuilder preview: {}在主动拖动时删除背景的功能,但当我们在开始拖动之前长时间按住该项目时,它仍然会出现。
Hel*_*rld -5
另一种方法:使用部分(这可能是重新排序时背景的原因)
var body: some View {
List {
ForEach(self.items, id: \.self) { item in
Section {
Text(item)
.padding(.all)
.listStyle(.plain)
.foregroundColor(.white)
.listRowBackground(Color.blue)
.cornerRadius(12)
}
.listSectionSeparator(.automatic)
.listSectionSeparatorTint(.clear)
.listStyle(.plain)
}
.onMove(perform: move)
}
.scrollContentBackground(.hidden)
}
func move(from source: IndexSet, to destination: Int) {
items.move(fromOffsets: source, toOffset: destination)
}
Run Code Online (Sandbox Code Playgroud)