我在 macOS 上使用 SwiftUI 的
ForEach
,并具有透明的 NSWindow
背景。滚动时显示重影。这是 SwiftUI 的错误还是错误的用法?我该如何解决它?
代码在这里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)
}
}
我也遇到了类似的问题。 我通过使用“window.hasShadow = false”解决了。