我试图将带有复选框的简单NSView
添加为NSOpenPanel
的附件视图,但是当我运行程序时,出现错误提示The Open/Save panel was supplied an accessory view with bad layout constraints, resulting in a view that is zero [height/width].
,这是我添加到视图的约束:
这是该复选框的约束:
以下是创建NSOpenPanel
的代码:
let dlgOpenSounds: NSOpenPanel = NSOpenPanel()
let optionsView = BatchAddOptionsView()
dlgOpenSounds.accessoryView = optionsView
dlgOpenSounds.accessoryView?.awakeFromNib()
let result = dlgOpenSounds.runModal()
if result == .OK {
// do stuff
}
有人知道我在做什么错吗?
答案 0 :(得分:1)
我遇到了同样的问题,代码中创建了类似的安排,最后解决了。我的实现是在自定义NSView子类中处理的,然后将其从显示面板的视图控制器中添加为NSOpenPanel的.accessoryView。
private func setup() {
hiddenFilesCheckbox = NSButton(checkboxWithTitle: "Show Hidden Files", target: self, action: #selector(hiddenFilesCheckboxValueChanged))
guard let checkbox = hiddenFilesCheckbox else {
os_log("Hidden files checkbox is nil")
return
}
addSubview(checkbox)
checkbox.translatesAutoresizingMaskIntoConstraints = false
checkbox.leadingAnchor.constraint(equalTo: self.leadingAnchor, constant: 12).isActive = true
checkbox.topAnchor.constraint(equalTo: self.topAnchor, constant: 12).isActive = true
self.heightAnchor.constraint(greaterThanOrEqualToConstant: frame.height).isActive = true
self.widthAnchor.constraint(greaterThanOrEqualToConstant: frame.width).isActive = true
}
“ hiddenFilesCheckbox”被声明为我的自定义NSView子类的属性。我使用了其他一些常量的硬编码值,但是这些值在我的测试中效果最好。我将openPanel传递给子类的初始化程序,以使用其框架来设置annexView的宽度。我将硬编码值40用作初始化程序中未包含的高度。在设置了具有这些约束的附件视图之后,警告将停止出现,并且附件视图将按需要/预期出现。
答案 1 :(得分:0)