首页 > 解决方案 > 如何在 MacOS 上滚动时修复 SwifyUI 列表的重影?

问题描述

我在透明背景ForEach下使用SwiftUIon ,它在滚动时显示重影,这是错误还是错误用法?我该如何解决?谢谢!MacOSNSWindowSwiftUI

代码在这里 https://github.com/someniceone/SwiftUIListGhosting

重影

我的代码:

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)
    }
}

标签: swiftmacosswiftuiswift5

解决方案


推荐阅读