删除 SwiftUI Mac 应用程序列表中右键单击行的轮廓
我正在使用 SwiftUI 构建 macOS 应用程序,并且在右键单击 List
项目时尝试删除(甚至掩盖)添加到该项目的边框。
默认情况下是这样的:
现在,右键单击并使用 contextMenu
视图修饰符:
我认为这是一个 NSTableView
怪癖,所以我尝试了这三篇 Stack Overflow 帖子中的方法:
我无法让其中任何一个工作,这可能是因为我无法子类化 NSTableView
,而只能覆盖其属性和带有扩展
的方法。到目前为止,我已经成功删除了默认的表格背景,例如:
extension NSTableView{
open override func viewDidMoveToWindow() {
super.viewDidMoveToWindow()
//Remove default table styles
backgroundColor = NSColor.clear
enclosingScrollView!.drawsBackground = false
selectionHighlightStyle = .none
}
}
有什么方法可以删除 SwiftUI 中的右键单击边框吗?我什至愿意用其他视图覆盖它,但我似乎无法在表格单元格周围的空间中绘制 SwiftUI 视图。
I'm building a macOS app with SwiftUI, and I'm trying to remove (or even cover up) the border added to a List
item when I right-click it.
Here it is by default:
Now with a right-click and a contextMenu
view modifier:
I figured this is an NSTableView
quirk, so I tried the approaches in these three Stack Overflow posts:
- Customize right click highlight on view-based NSTableView
- NSTableView with menu, how to change the border color with right click?
- Disabling the NSTableView row focus ring
- NSTableView: blue outline on right-clicked rows
I couldn't get any of those to work, and that may be due to the fact that I can't subclass an NSTableView
, but can only override its properties and methods with an extension
. Here's what I have so far that successfully removes the default table background and such:
extension NSTableView{
open override func viewDidMoveToWindow() {
super.viewDidMoveToWindow()
//Remove default table styles
backgroundColor = NSColor.clear
enclosingScrollView!.drawsBackground = false
selectionHighlightStyle = .none
}
}
Is there any way to remove that right-click border in SwiftUI? I'm even open to covering it with other views, but I can't seem to draw SwiftUI views in that space around the table cell.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我找到了解决这个问题的方法。我将
List
放入ZStack
中,然后将其opacity
设置为零。然后,我构建了同一列表的完全自定义版本,但使用 LazyVStack:每个列表都绑定到同一模型,因此如果我单击一条消息来选择它自定义 UI,在不可见的
List
中选择相同的内容。List
中表格使用的所有键盘快捷键看起来都适用于自定义版本。那么这如何解决我原来的问题呢?您可以右键单击自定义
MessageItemView
,单元格周围的默认环是不可见的,但contextMenu
仍然有效(它在我的MessageItemView
中定义) >)。这并不像我想要的那么优雅,但是能够 100% 控制 UI,同时仍然获得
List
中免费提供的所有键盘控件,这很好。I found a workaround for this. I put my
List
in aZStack
and then set itsopacity
to zero. I then built out a fully custom version of the same list, but usingLazyVStack
:Each list is bound to the same model, so if I click a
message
to select it in the custom UI, the same thing gets selected in the invisibleList
. All the keyboard shortcuts that come with table use in aList
look like they are working on the custom version.So how does this solve my original problem? You can right-click on the custom
MessageItemView
and the default ring around the cell is invisible, but thecontextMenu
still works (it's defined inside myMessageItemView
).This isn't as elegant as I'd like, but it's nice to have 100% control over the UI but still get all the keyboard controls that come for free with a
List
.你可以用这个。它将重新加载您的列表。我正在寻找另一种方法,但这可行。
You can use this. It will reload your List. I'm looking for another approach but this works.