我正在尝试使用NSTableView作为内容构建NSScrollView,但是却收到一条错误消息,说我缺少Table上的内容,而且不确定是否丢失了什么。我在布局中使用的是SnapKit,错误消息为Detected missing constraints for <NSTableView: 0x10100aaf0>. It cannot be placed because there are not enough constraints to fully define the size and origin.
。
我对ViewController的代码是:
class MainViewController: NSViewController {
let table: NSTableView = {
let t = NSTableView(frame: .zero)
t.headerView?.isHidden = false
t.selectionHighlightStyle = .regular
t.translatesAutoresizingMaskIntoConstraints = false
t.autoresizingMask = [.height, .width]
t.usesAlternatingRowBackgroundColors = true
let col1 = NSTableColumn(identifier: NSUserInterfaceItemIdentifier("Name"))
col1.headerCell = NSTableHeaderCell(textCell: "Name")
t.addTableColumn(col1)
return t
}()
required init() {
super.init(nibName: nil, bundle: nil)
}
required init?(coder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
override func loadView() {
view = NSView(frame: .zero)
let scrollView = NSScrollView(frame: .zero)
scrollView.hasVerticalScroller = true
scrollView.autohidesScrollers = false
scrollView.documentView = table
view.addSubview(scrollView)
/// Layout
scrollView.snp.makeConstraints { make in
make.edges.equalToSuperview().inset(Padding.large)
}
}
大多数在线指南都旨在使用笔尖,但我正在尝试以编程方式进行操作。有人知道它是怎么做的吗?我使用XCode 9.2和Swift4。谢谢。
答案 0 :(得分:1)
我的猜测是您忘记为tableView
本身添加约束,以描述应如何在封闭的滚动视图中进行布局。
您可以在DETECTED_MISSING_CONSTRAINTS
上设置符号断点。 Here is a video显示如何设置符号断点,以防万一您从未遇到过。
它应该让您更深入地了解到底是什么错误。约束是一项棘手的事情,可能很难在不了解全部情况的情况下进行推理。
答案 1 :(得分:0)
我只是通过让表视图使用由自动调整大小掩码自动设置的约束来使错误静音。 SnapKit会自动设置行translatesAutoresizingMaskIntoConstraints = false
,因此将其设置为true
似乎可行。
let table: NSTableView = {
let t = NSTableView(frame: .zero)
t.headerView?.isHidden = false
t.selectionHighlightStyle = .regular
t.translatesAutoresizingMaskIntoConstraints = true
t.usesAlternatingRowBackgroundColors = true
let col1 = NSTableColumn(identifier: NSUserInterfaceItemIdentifier("Name"))
col1.headerCell = NSTableHeaderCell(textCell: "Name")
t.addTableColumn(col1)
return t
}()