问题
Is there away to allow reordering of rows in a SwiftUI List whist NOT in edit mode?
That is to give rows a right hand hamburger menu icon which they can use to re-order a row? (like which is possible in edit mode)
回答1:
If I understood your question correctly, here is how it is possible to be done:
import SwiftUI
struct TestEditModeCustomRelocate: View {
@State private var objects = ["1", "2", "3"]
@State var editMode: EditMode = .active
var body: some View {
List {
ForEach(objects, id: \.self) { object in
Text("Row \(object)")
}
.onMove(perform: relocate)
}
.environment(\.editMode, $editMode)
}
func relocate(from source: IndexSet, to destination: Int) {
objects.move(fromOffsets: source, toOffset: destination)
}
}
struct TestEditModeCustomRelocate_Previews: PreviewProvider {
static var previews: some View {
TestEditModeCustomRelocate()
}
}
来源:https://stackoverflow.com/questions/58765661/how-to-allow-reordering-of-rows-in-a-swiftui-list-whist-not-in-edit-mode