I'm trying to implement simple list with draggable cells (iOS 15). For now I have two base solutions: List and ScrollView. None of this methods doesn't lead me to appropriate result.
First of all I'll provide some code:
List item view:
struct ListItemView: View {
let index: Int
var body: some View {
HStack {
Text("Item \(index)")
.foregroundColor(.white)
Spacer()
}
.padding()
.background(Color.gray)
.clipShape(RoundedRectangle(cornerRadius: 16.0))
}
}
First solution (using List):
struct ContentView: View {
var body: some View {
List(0 ..< 10) { i in
ListItemView(index: i)
.onDrag {
NSItemProvider(object: String(describing: "item_\(i)") as NSString)
}
.padding(.bottom, 8.0)
.listRowBackground(Color.clear)
.listRowSeparator(.hidden)
.listRowInsets(EdgeInsets(top: 0, leading: 8, bottom: 0, trailing: 8))
}
.background(Color.black)
.listStyle(PlainListStyle())
}
}
The result is fine, but when I start dragging action, I see some white rect around my cell, which is not what I want:
Second solution (using ScrollView)
struct ContentView: View {
var body: some View {
ScrollView {
ForEach(0 ..< 10) { i in
ListItemView(index: i)
.onDrag {
NSItemProvider(object: String(describing: "item_\(i)") as NSString)
}
}
}
.background(Color.black)
}
}
The result is a bit different: we don't see any coloured rects (I suppose, this is because we are not using List and there is different mechanism of drag&drop feature). Also there is scaled preview and shape without rounded corners.
So, the desirable behaviour is: a) the size of preview is the same as original size of list item b) there is rounded-corners shape without white frame
How can I achieve it?