NSTableView 在第一行上方有不需要的 space
NSTableView has unwanted space above first row
我有一个 NSTableView,它的第一行从顶部向下推 10 pt。 Headers 已关闭,没有组行,单元格间距为 0,封闭滚动视图的内容插入为 0。
已更新
这里有一个 sample project 演示了这个问题。
这是视图层次结构调试器的截图:
这是在视图层次调试器中暂停时第一行 NSTableRowView
的垂直约束:
我尝试实现委托的 tableView(_:didAdd:forRow:) and inspecting the constraints, first with constraintsAffectingLayout(for:):
[<NSLayoutConstraint:0x60000390bd40 'NSTableRowView_Encapsulated_Layout_Height' NSTableRowView:0x7fdb12e26eb0.height == 24 priority:500 (active)>]
然后打印所有行视图的constraints:
- 0 : <NSLayoutConstraint:0x60000390bcf0 'NSTableRowView_Encapsulated_Layout_Width' NSTableRowView:0x7fdb12e26eb0.width == 329 priority:500 (active)>
- 1 : <NSLayoutConstraint:0x60000390bd40 'NSTableRowView_Encapsulated_Layout_Height' NSTableRowView:0x7fdb12e26eb0.height == 24 priority:500 (active)>
- 2 : <NSAutoresizingMaskLayoutConstraint:0x60000390bbb0 h=--& v=-&- InlineCell.minX == 16 (active, names: InlineCell:0x7fdb12e283a0, '|':NSTableRowView:0x7fdb12e26eb0 )>
- 3 : <NSAutoresizingMaskLayoutConstraint:0x60000390bc00 h=--& v=-&- InlineCell.width == 297 (active, names: InlineCell:0x7fdb12e283a0 )>
- 4 : <NSAutoresizingMaskLayoutConstraint:0x60000390bc50 h=--& v=-&- InlineCell.minY == 0 (active, names: InlineCell:0x7fdb12e283a0, '|':NSTableRowView:0x7fdb12e26eb0 )>
- 5 : <NSAutoresizingMaskLayoutConstraint:0x60000390bca0 h=--& v=-&- V:[InlineCell]-(0)-| (active, names: InlineCell:0x7fdb12e283a0, '|':NSTableRowView:0x7fdb12e26eb0 )>
单元格的 minY
约束设置为 0,但该行正在使用自动调整掩码。 table 使用简单的 diffable 数据源:
NSTableViewDiffableDataSourceReference(tableView: table) { tableView, column, row, item in
guard let cell = tableView.makeView(withIdentifier: inlineCellIdentifier, owner: self) as? NSTableCellView else {
preconditionFailure("Failed to create results cell")
}
cell.textField?.textColor = self.themeAttributes.color
cell.textField?.font = self.themeAttributes.font
cell.textField?.stringValue = self.displayString(for: item)
return cell
}
唯一实现的委托方法是 tableView(_:heightOfRow:)。 table 将自身与同级文本视图的行对齐,以便从那里获取行高:
func tableView(_ tableView: NSTableView, heightOfRow row: Int) -> CGFloat {
guard let layoutManager = editor?.layoutManager else {
preconditionFailure("Missing layout manager in editor")
}
return height(of: row, in: layoutManager)
}
这似乎很明显,但我不明白为什么 table 会强制其行像这样偏移。我在这个论坛上发现了很多问题,询问如何 在 table 的顶部插入 一个间隙,而不是如何删除一个。有什么建议吗?
这不是错误,如某些评论所述,这是 NSTableView
在 Big Sur 中的一种新工作方式(实际上有一个错误,但在其他地方,见下文)。免费的 Pascal 站点包含 nice overview of what's new.
新 NSTableView
属性
macOS Big Sur 引入了新的 NSTableView
属性:
style
文档:
The default value for this property is NSTableView.Style.automatic
in macOS 11. Apps that link to previous macOS versions default to NSTableView.Style.fullWidth
.
effectiveStyle
文档:
If the style
property value is NSTableView.Style.automatic
, then this property contains the resolved style.
The system resolves the table view style in the following manner:
- If the table view is in a sidebar split-view controller item,
effectiveStyle
resolves to NSTableView.Style.sourceList
.
- If the table’s scroll view has a border,
effectiveStyle
resolves to NSTableView.Style.fullWidth
.
- Otherwise
effectiveStyle
resolves to NSTableView.Style.inset
. However, if the table needs extra space to fit its column cells, effectiveStyle
resolves to NSTableView.Style.fullWidth
.
您的 table 视图在 Main.storyboard 中的样式设置为 .automatic
。这意味着有效样式解析为 .inset
-> 内容周围 10pt(基于 .automatic
文档中的规则)。
打开带有选定行的视图调试器。可以看到.inset
样式效果:
使用 .fullWidth
删除 10pt 插图。
您还可以测试 .automatic
样式行为 - 尝试向滚动视图添加边框。解析为 .fullWidth
.
错误和解决方法
您可能试图在 Interface Builder 中将 table 视图样式设置为全宽。它不起作用。无论您选择什么值,它的行为都像 .automatic
.
将以下行添加到您的代码中以解决此问题:
tableView?.style = .fullWidth
现在按预期工作:
报告为 FB8258910。
我有一个 NSTableView,它的第一行从顶部向下推 10 pt。 Headers 已关闭,没有组行,单元格间距为 0,封闭滚动视图的内容插入为 0。
已更新
这里有一个 sample project 演示了这个问题。
这是视图层次结构调试器的截图:
这是在视图层次调试器中暂停时第一行 NSTableRowView
的垂直约束:
我尝试实现委托的 tableView(_:didAdd:forRow:) and inspecting the constraints, first with constraintsAffectingLayout(for:):
[<NSLayoutConstraint:0x60000390bd40 'NSTableRowView_Encapsulated_Layout_Height' NSTableRowView:0x7fdb12e26eb0.height == 24 priority:500 (active)>]
然后打印所有行视图的constraints:
- 0 : <NSLayoutConstraint:0x60000390bcf0 'NSTableRowView_Encapsulated_Layout_Width' NSTableRowView:0x7fdb12e26eb0.width == 329 priority:500 (active)>
- 1 : <NSLayoutConstraint:0x60000390bd40 'NSTableRowView_Encapsulated_Layout_Height' NSTableRowView:0x7fdb12e26eb0.height == 24 priority:500 (active)>
- 2 : <NSAutoresizingMaskLayoutConstraint:0x60000390bbb0 h=--& v=-&- InlineCell.minX == 16 (active, names: InlineCell:0x7fdb12e283a0, '|':NSTableRowView:0x7fdb12e26eb0 )>
- 3 : <NSAutoresizingMaskLayoutConstraint:0x60000390bc00 h=--& v=-&- InlineCell.width == 297 (active, names: InlineCell:0x7fdb12e283a0 )>
- 4 : <NSAutoresizingMaskLayoutConstraint:0x60000390bc50 h=--& v=-&- InlineCell.minY == 0 (active, names: InlineCell:0x7fdb12e283a0, '|':NSTableRowView:0x7fdb12e26eb0 )>
- 5 : <NSAutoresizingMaskLayoutConstraint:0x60000390bca0 h=--& v=-&- V:[InlineCell]-(0)-| (active, names: InlineCell:0x7fdb12e283a0, '|':NSTableRowView:0x7fdb12e26eb0 )>
单元格的 minY
约束设置为 0,但该行正在使用自动调整掩码。 table 使用简单的 diffable 数据源:
NSTableViewDiffableDataSourceReference(tableView: table) { tableView, column, row, item in
guard let cell = tableView.makeView(withIdentifier: inlineCellIdentifier, owner: self) as? NSTableCellView else {
preconditionFailure("Failed to create results cell")
}
cell.textField?.textColor = self.themeAttributes.color
cell.textField?.font = self.themeAttributes.font
cell.textField?.stringValue = self.displayString(for: item)
return cell
}
唯一实现的委托方法是 tableView(_:heightOfRow:)。 table 将自身与同级文本视图的行对齐,以便从那里获取行高:
func tableView(_ tableView: NSTableView, heightOfRow row: Int) -> CGFloat {
guard let layoutManager = editor?.layoutManager else {
preconditionFailure("Missing layout manager in editor")
}
return height(of: row, in: layoutManager)
}
这似乎很明显,但我不明白为什么 table 会强制其行像这样偏移。我在这个论坛上发现了很多问题,询问如何 在 table 的顶部插入 一个间隙,而不是如何删除一个。有什么建议吗?
这不是错误,如某些评论所述,这是 NSTableView
在 Big Sur 中的一种新工作方式(实际上有一个错误,但在其他地方,见下文)。免费的 Pascal 站点包含 nice overview of what's new.
新 NSTableView
属性
macOS Big Sur 引入了新的 NSTableView
属性:
style
文档:
The default value for this property is
NSTableView.Style.automatic
in macOS 11. Apps that link to previous macOS versions default toNSTableView.Style.fullWidth
.
effectiveStyle
文档:
If the
style
property value isNSTableView.Style.automatic
, then this property contains the resolved style.
The system resolves the table view style in the following manner:
- If the table view is in a sidebar split-view controller item,
effectiveStyle
resolves toNSTableView.Style.sourceList
.- If the table’s scroll view has a border,
effectiveStyle
resolves toNSTableView.Style.fullWidth
.- Otherwise
effectiveStyle
resolves toNSTableView.Style.inset
. However, if the table needs extra space to fit its column cells,effectiveStyle
resolves toNSTableView.Style.fullWidth
.
您的 table 视图在 Main.storyboard 中的样式设置为 .automatic
。这意味着有效样式解析为 .inset
-> 内容周围 10pt(基于 .automatic
文档中的规则)。
打开带有选定行的视图调试器。可以看到.inset
样式效果:
使用 .fullWidth
删除 10pt 插图。
您还可以测试 .automatic
样式行为 - 尝试向滚动视图添加边框。解析为 .fullWidth
.
错误和解决方法
您可能试图在 Interface Builder 中将 table 视图样式设置为全宽。它不起作用。无论您选择什么值,它的行为都像 .automatic
.
将以下行添加到您的代码中以解决此问题:
tableView?.style = .fullWidth
现在按预期工作:
报告为 FB8258910。