SwiftUI List listRowBackground opacity bug while dragging

61 views Asked by At

I found a bug in SwiftUI's List view where if the listRowBackground is set, the items will sometimes be opaque while dragging to reorder (instead of the default transparent views). Has anyone else encountered this or found any work-arounds for it?

demonstration of bug

I'm currently using Xcode 15 and iOS 17, and the issue is reproducible, as long as you are patient, since it is very sporadic.

Here is the code for the reproducible example:

struct ContentView: View {
    @State private var editMode: EditMode = .inactive
    
    var body: some View {
        VStack {
            Button(editMode == .active ? "Done" : "Edit") {
                if editMode == .active {
                    editMode = .inactive
                } else {
                    editMode = .active
                }
            }
            
            List {
                ForEach(1 ..< 11) { index in
                    Text("Item Number \(index)")
                }
                .onMove(perform: { _, _ in })
                .listRowBackground(Color.white)
            }
            .environment(\.editMode, $editMode)
        }
    }
}
1

There are 1 answers

1
Stornu2 On

May be this can help you:

struct ContentView: View {
    @State private var editMode: EditMode = .inactive
    
    var body: some View {
        VStack {
            Button(editMode == .active ? "Done" : "Edit") {
                if editMode == .active {
                    editMode = .inactive
                } else {
                    editMode = .active
                }
            }
            
            List {
                ForEach(1 ..< 11) { index in
                    Text("Item Number \(index)")
                        .background(Color.white) // Set background color directly
                }
                .onMove(perform: { _, _ in })
                .id(UUID()) // Force refresh
            }
            .environment(\.editMode, $editMode)
        }
    }
}