gpt4 book ai didi

swift - NSGridView 困难

转载 作者:搜寻专家 更新时间:2023-10-31 22:17:47 28 4
gpt4 key购买 nike

我在我的 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 填满了整个 View ,但复选框确实关闭了。正如你在图片中看到的那样。

此外,我希望内容不会填满整个单元格,让所有内容看起来更居中。

我该如何解决这个问题?

enter image description here

最佳答案

如果将行对齐更改为 .lastBaseline,它将解决 NSButtonNSTextField 的垂直对齐问题。如果你想要更多的间距(我猜这就是你的意思“我希望内容不会填满整个单元格”),你可以使用 columnSpacingrowSpacing NSGridView 的属性。如果您随后还在“真实”内容周围添加 NSGridCell.emptyContentView 实例,您应该能够实现以下效果。 enter image description here

这是我建议的更改后的新代码:

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)
}
}

关于swift - NSGridView 困难,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52045470/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com