[在SwiftUI中,当我进入编辑模式但不使用onDelete时,如何避免行内容缩进列表中?这是当前发生这种情况时我的行内容缩进的方式,好像“删除”按钮将显示在左侧,但是我没有使用onDelete,所以那里没有按钮。
动画GIF
此处提取代码:
var body: some View {
VStack{
List() {
ForEach(gcTasks) { gcTask in
HStack {
GCTaskRow(withGcTask: gcTask, haha: "")
}
}
// .onDelete(perform: self.deleteTask) // NOTE: HAVE REMOVED
.onMove(perform: self.move)
}
}
.environment(\.editMode, listInEditMode ? .constant(.active) : .constant(.inactive))
}
背景-实际上总是想一直处于编辑模式,即总是可以选择上下拖动行,但是永远不会使用Delete,因此要查看onDelete的所有痕迹,在这种情况下是自动缩进。 。
也许值得向Apple提交请求,但这是默认行为。现在可以采用以下解决方法。经过Xcode 11.4 / iOS 13.4的测试。
struct DemoListEditMode: View {
@State var items = ["a","b","c","d","e"]
var body: some View {
List {
ForEach(items, id: \.self) { z in
HStack {
Image(systemName: "checkmark.square") // just for demo
Text("\(z)")
}
}.onMove(perform: self.move)
.listRowInsets(EdgeInsets(top: 0, leading: -24, // workaround !!
bottom: 0, trailing: 0))
}
.environment(\.editMode, .constant(.active)) // persistent edit mode
}
func move(source: IndexSet, destination: Int) {
self.items.move(fromOffsets: source, toOffset: destination)
}
}