更新时间:2023-12-04 19:26:16
更新:我找到了一种更好的方法来删除列表的背景而不影响整个应用程序:使用 自省.
Update: I found a much better way to remove a list's background without affecting the whole app: by using Introspect.
import Introspect
import SwiftUI
extension List {
/// List on macOS uses an opaque background with no option for
/// removing/changing it. listRowBackground() doesn't work either.
/// This workaround works because List is backed by NSTableView.
func removeBackground() -> some View {
return introspectTableView { tableView in
tableView.backgroundColor = .clear
tableView.enclosingScrollView!.drawsBackground = false
}
}
}
用法:
List {
ForEach(items) { item in
...
}
}.removeBackground()
旧答案:
@Asperi 的回答 有效,但仅在调整窗口大小之前有效.这是覆盖 List
颜色的另一种解决方法:
@Asperi's answer works, but only until the window is resized. Here's another workaround for overriding List
's color:
extension NSTableView {
open override func viewDidMoveToWindow() {
super.viewDidMoveToWindow()
backgroundColor = NSColor.clear
enclosingScrollView!.drawsBackground = false
}
}
一个潜在的缺点是这会影响应用中的所有列表.
A potential downside is that this will affect all lists in the app.