Reorder SwiftUI List in Mac App with LazyVStack

701 Views Asked by At

Here is a simple macOS demo app that compares using a SwiftUI List and a LazyVStack. I prefer to use a LazyVStack because it's easier to customize the style. But it seems some of the built-in functionality is missing, such as reordering list items (as shown below)

import SwiftUI

class MyData: ObservableObject{
  @Published var items = ["One", "Two", "Three", "Four", "Five"]
}

struct ContentView: View {
  @StateObject var model = MyData()

  var body: some View {
    HStack{
      //--- List ---
      VStack{
        Text("List")
          .fontWeight(.bold)
        List{
          ForEach(model.items, id: \.self){ item in
            Text(item)
          }
          .onMove(perform: move)
        }
      }
      //--- LazyVStack ---
      VStack{
        Text("LazyVStack")
          .fontWeight(.bold)
        ScrollView {
          LazyVStack {
            ForEach(model.items, id: \.self){ item in
              Text(item)
            }
            .onMove(perform: move)
          }
        }
      }
    }
  }
  //Reorder List
  func move(from source: IndexSet, to destination: Int) {
    model.items.move(fromOffsets: source, toOffset: destination )
  }
}

enter image description here

The reordering of items works fine in the List example. But nothing happens in the LazyVStack.

Is there a way to get the .onMove functionality to work in the LazyVStack instance? Or do I have to implement a full bevy of custom .onDrag stuff with NSItemProvider?

0

There are 0 best solutions below