sách gpt4 ăn đã đi

swift - NSGridView 困难

In lại 作者:搜寻专家 更新时间:2023-10-31 22:17:47 28 4
mua khóa gpt4 giày 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 ,但复选框确实关闭了。正如你在图片中看到的那样。

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

Làm thế nào tôi có thể giải quyết vấn đề này?

nhập mô tả hình ảnh ở đây

câu trả lời hay nhất

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

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

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
Chứng chỉ ICP Bắc Kinh số 000000
Hợp tác quảng cáo: 1813099741@qq.com 6ren.com
Xem sitemap của VNExpress