How to fix the ghosting of SwifyUI List while scrolling on MacOS?

Viewed 74

I'm using ForEach of SwiftUI on MacOS with transparent NSWindow background, it shows ghosting while scrolling, is this a bug of SwiftUI or a wrong usage? How can I fix it? Thank you!

Code is here https://github.com/someniceone/SwiftUIListGhosting

ghosting

My Code:

struct ScriptListRow: View {

    var script: Script
    var index: Int

    var body: some View {
        HStack {
            Image(script.iconName)
                    .resizable()
                    .frame(width: 20, height: 20, alignment: .center)
            Text(script.filename)
                    .foregroundColor(Color.white)
            Spacer()
        }
                .padding(.horizontal, 5)
                .padding(.vertical, 8)
    }
}

struct ScriptList: View {

    @State private var selectedId: Int?

    let withIndex = scripts.enumerated().map({ $0 })

    var body: some View {
        ScrollView {
            VStack(spacing: 0) {
                ForEach(withIndex, id: \.element.id) { index, script in
                    ScriptListRow(script: script, index: index)
                    .background(selectedId == index ? Color.accentColor : (index % 2 == 1 ? Color.clear: Color(white: 0.1, opacity: 0.2)))
                    .onTapGesture(perform: {
                        selectedId = index
                    })
                }
            }
        }
                .frame(width: .infinity, height: 300, alignment: .top)
    }
}

0 Answers
Related