macOS 上的 SwiftUI ListStyle
SwiftUI ListStyle on macOS
在 macOS 上,使用 SwiftUI,关于列表:
我需要修改List的背景颜色。
使用视图调试器,我看到列表内部有一个 ListCoresScrollView,一个内部 _NSScrollViewContentBackgroundView
和一个 NSVisualEffectView。默认情况下,该视图具有白色背景。我想更改该特定视图(例如变得透明),因为它似乎是一个内部视图。
如何修改列表的背景视图(在 macOS 中,不是 IOS)?
顺便说一句,我尝试使用 SidebarListStyle()。这确实提供了我想要的背景,但是......在 SidebarListStyle 中拖动行为是不同的(不需要的)。所以我的问题的替代解决方案可能是 "how to modify dragging behavior on a SidebarListStyle to mimic the default dragging behavior"?
我花了很多时间,但我想不出一个合适的解决方案。谁能提供可行的解决方案?
更新 #2(大约 30 分钟后)...
这有效:)
extension NSTableView {
open override func viewDidMoveToWindow() {
super.viewDidMoveToWindow()
// set the background color of every list
backgroundColor = .clear
enclosingScrollView?.drawsBackground = false
enclosingScrollView?.backgroundColor = .clear
enclosingScrollView?.autohidesScrollers = true
enclosingScrollView?.verticalScroller = MyScroller()
}
}
class MyScroller: NSScroller {
override func draw(_ dirtyRect: NSRect) {
self.drawKnob()
}
}
原回答
不确定您是否仍在寻找答案,但这是我设法得到的最接近的答案。仍然无法使滚动条(尤其是背景)真正透明。但是,比我更近了。
在你的项目中添加一个 swift 文件(现在,我只是把它放在我的 ContentView.swift 文件的顶部)。
extension NSTableView {
open override func viewDidMoveToWindow() {
super.viewDidMoveToWindow()
// set the background color of every list
backgroundColor = .clear
headerView = nil
enclosingScrollView?.drawsBackground = false
enclosingScrollView?.backgroundColor = .clear
enclosingScrollView?.hasHorizontalScroller = false
enclosingScrollView?.autohidesScrollers = true
}
}
据我了解,应该 有效。但是,事实并非如此。我可能不需要所有这些设置,但我会继续添加直到我可以完全正常工作。
HTH - 如果您已完全解决问题,请告诉我。
我进一步研究了 RichS 的答案,发现在我的项目中满足以下条件:
extension NSTableView {
open override func viewDidMoveToWindow() {
super.viewDidMoveToWindow()
backgroundColor = .clear
enclosingScrollView?.drawsBackground = false
}
}
YMMV,所以如果您的滚动没有按预期工作,请查看上面 RichS 的回答。
在 macOS 上,使用 SwiftUI,关于列表:
我需要修改List的背景颜色。
使用视图调试器,我看到列表内部有一个 ListCoresScrollView,一个内部 _NSScrollViewContentBackgroundView
和一个 NSVisualEffectView。默认情况下,该视图具有白色背景。我想更改该特定视图(例如变得透明),因为它似乎是一个内部视图。
如何修改列表的背景视图(在 macOS 中,不是 IOS)?
顺便说一句,我尝试使用 SidebarListStyle()。这确实提供了我想要的背景,但是......在 SidebarListStyle 中拖动行为是不同的(不需要的)。所以我的问题的替代解决方案可能是 "how to modify dragging behavior on a SidebarListStyle to mimic the default dragging behavior"?
我花了很多时间,但我想不出一个合适的解决方案。谁能提供可行的解决方案?
更新 #2(大约 30 分钟后)...
这有效:)
extension NSTableView {
open override func viewDidMoveToWindow() {
super.viewDidMoveToWindow()
// set the background color of every list
backgroundColor = .clear
enclosingScrollView?.drawsBackground = false
enclosingScrollView?.backgroundColor = .clear
enclosingScrollView?.autohidesScrollers = true
enclosingScrollView?.verticalScroller = MyScroller()
}
}
class MyScroller: NSScroller {
override func draw(_ dirtyRect: NSRect) {
self.drawKnob()
}
}
原回答
不确定您是否仍在寻找答案,但这是我设法得到的最接近的答案。仍然无法使滚动条(尤其是背景)真正透明。但是,比我更近了。
在你的项目中添加一个 swift 文件(现在,我只是把它放在我的 ContentView.swift 文件的顶部)。
extension NSTableView {
open override func viewDidMoveToWindow() {
super.viewDidMoveToWindow()
// set the background color of every list
backgroundColor = .clear
headerView = nil
enclosingScrollView?.drawsBackground = false
enclosingScrollView?.backgroundColor = .clear
enclosingScrollView?.hasHorizontalScroller = false
enclosingScrollView?.autohidesScrollers = true
}
}
据我了解,应该 有效。但是,事实并非如此。我可能不需要所有这些设置,但我会继续添加直到我可以完全正常工作。
HTH - 如果您已完全解决问题,请告诉我。
我进一步研究了 RichS 的答案,发现在我的项目中满足以下条件:
extension NSTableView {
open override func viewDidMoveToWindow() {
super.viewDidMoveToWindow()
backgroundColor = .clear
enclosingScrollView?.drawsBackground = false
}
}
YMMV,所以如果您的滚动没有按预期工作,请查看上面 RichS 的回答。