我在我的macOS应用程序的设置面板中使用了NSGridView
。我这样设置:
class GeneralViewController: RootViewController {
private var gridView: NSGridView?
override func loadView() {
super.loadView()
let restaurantLabel = NSTextField()
restaurantLabel.stringValue = "Restaurant"
let restaurantButton = NSPopUpButton()
restaurantButton.addItems(withTitles: ["A", "B", "C"])
let updatesLabel = NSTextField()
updatesLabel.stringValue = "Updates"
updatesLabel.isEditable = false
updatesLabel.isBordered = false
updatesLabel.isBezeled = false
let updatesButton = NSButton(checkboxWithTitle: "Automatically pull in updates from WordPress", target: nil, action: nil)
let empty = NSGridCell.emptyContentView
gridView = NSGridView(views: [
[restaurantLabel, restaurantButton],
[updatesLabel, updatesButton]
])
gridView?.wantsLayer = true
gridView?.layer?.backgroundColor = NSColor.red.cgColor
gridView?.column(at: 0).xPlacement = .trailing
gridView?.rowAlignment = .firstBaseline
gridView?.setContentHuggingPriority(NSLayoutConstraint.Priority(rawValue: 600), for: .horizontal)
gridView?.setContentHuggingPriority(NSLayoutConstraint.Priority(rawValue: 600), for: .vertical)
view.addSubview(gridView!)
}
override func viewDidLayout() {
super.viewDidLayout()
gridView?.frame = NSRect(x: 0, y: 0, width: view.frame.size.width, height: view.frame.size.height)
}
}
现在,当我运行它时,NSGridView
填满整个视图,但复选框真的关闭了。正如您在图像中看到的那样。
此外,我喜欢内容不能填满整个单元格,使它看起来更加集中。
我怎么解决这个问题?
如果将行对齐更改为.lastBaseline
,它将解决NSButton
和NSTextField
的垂直对齐问题。如果你想要更多的间距(我猜你的意思是“我喜欢内容不能填满整个单元格”),你可以使用columnSpacing
的rowSpacing
和NSGridView
属性。如果您还在“真实”内容周围添加NSGridCell.emptyContentView
实例,您应该能够达到以下效果。
这是我建议的更改后的新代码:
class GeneralViewController: RootViewController {
private var gridView: NSGridView?
override func loadView() {
super.loadView()
let restaurantLabel = NSTextField()
restaurantLabel.stringValue = "Restaurant"
let restaurantButton = NSPopUpButton()
restaurantButton.addItems(withTitles: ["A", "B", "C"])
let updatesLabel = NSTextField()
updatesLabel.stringValue = "Updates"
updatesLabel.isEditable = false
updatesLabel.isBordered = false
updatesLabel.isBezeled = false
let updatesButton = NSButton(checkboxWithTitle: "Automatically pull in updates from WordPress", target: nil, action: nil)
let empty = NSGridCell.emptyContentView
gridView = NSGridView(views: [
[empty],
[empty, restaurantLabel, restaurantButton, empty],
[empty, updatesLabel, updatesButton, empty],
[empty],
[empty]
])
gridView?.wantsLayer = true
gridView?.layer?.backgroundColor = NSColor.red.cgColor
gridView?.column(at: 0).xPlacement = .trailing
gridView?.rowAlignment = .lastBaseline
gridView?.columnSpacing = 20
gridView?.rowSpacing = 20
gridView?.setContentHuggingPriority(NSLayoutConstraint.Priority(rawValue: 600), for: .horizontal)
gridView?.setContentHuggingPriority(NSLayoutConstraint.Priority(rawValue: 600), for: .vertical)
view.addSubview(gridView!)
}
override func viewDidLayout() {
super.viewDidLayout()
gridView?.frame = NSRect(x: 0, y: 0, width: view.frame.size.width, height: view.frame.size.height)
}
}
除非你有使用NSGridView
的其他原因,我建议使用NSTableView
。