1

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?

2
  • .onMove and .onDelete commands are only available embedded in Lists :/ Commented Mar 2, 2021 at 0:21
  • this post uses move with animation, LazyVStack and Drag & Drop Reorder List avitsadok.medium.com/… Commented Oct 7, 2021 at 21:32

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.