- Java 双重比较
- java - 比较器与 Apache BeanComparator
- Objective-C 完成 block 导致额外的方法调用?
- database - RESTful URI 是否应该公开数据库主键?
我在我的 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?
câu trả lời hay nhất
如果将行对齐更改为 .lastBaseline
,它将解决 NSButton
Và NSTextField
的垂直对齐问题。如果你想要更多的间距(我猜这就是你的意思“我希望内容不会填满整个单元格”),你可以使用 columnSpacing
Và 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)
}
}
关于swift - NSGridView 困难,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52045470/
由于 NSGridView 在 Interfacebuilder 中不可用,我尝试以编程方式创建一个。我尝试过这样的: class ViewController: NSViewController
我试图通过继承 NSGridView 并覆盖其 draw 方法来设置它的背景颜色,如下所示: class GridViewGreen: NSGridView { override func draw(
我在我的 macOS 应用程序的设置面板中使用 NSGridView。我是这样设置的: class GeneralViewController: RootViewController { pr
我想使用 NSGridView 来构建首选项窗口,但似乎我无法在 Storyboard中使用/布局它。我错过了什么吗? 最佳答案 从 Xcode 10 开始,NSGridView 可在 Interfa
我尝试使用 NSGridView 来布局 NSTableCellView 像这样 class GridItemCV: NSTableCellView { var grid : NSGridView?
我到处都找过了,但没有找到太多用于跨越 NSGridView 或 NSTableView 的列或行的内置功能。此时我想我必须重写某些方法或寻找另一个控件。我在 OSX 中工作的时间并不长,更糟糕的是我
我这样定义了一个 GridView : let lb01 = NSTextField(labelWithString: "01") .... let gridView = NSGridView(vie
我看到了如何将行中的不同列数设置为 NSGridView 的答案以编程方式。像这样: ______________ | | | | cell | cell | |______
Tôi là một lập trình viên xuất sắc, rất giỏi!