我正在嘗試NSComboBox在 Mac 應用程式中進行布局,但出現了一些奇怪的行為。實際上,我有一個視圖,它有一個NSComboBox作為子視圖以及其他一些子視圖的視圖,但是我創建了一個簡單的示例來顯示我所看到的相同問題。
出于某種原因,文本欄位并不像我期望的那樣完全垂直居中。
這是我的TestComboBox:

這是一個 Apple NSComboBox(注意文本垂直居中,并且在突出顯示顏色之前有少量前導間距):

我創建了一個簡單的例子來說明這個問題:
class TestComboBox: NSView {
private let comboBox = NSComboBox(labelWithString: "")
override init(frame frameRect: NSRect) {
super.init(frame: frameRect)
commonInit()
}
required init?(coder: NSCoder) {
super.init(coder: coder)
commonInit()
}
private func commonInit() {
comboBox.isEditable = true
addSubview(comboBox)
}
override func layout() {
comboBox.sizeToFit()
comboBox.frame = CGRect(x: 0, y: 0, width: bounds.width, height: comboBox.bounds.height)
}
}
如果您將上述視圖添加到基本的 Mac 應用程式(故事板)并將其固定到具有以下約束的視圖控制器:

I think I'm trying to layout the combo box correctly, but I'm not sure why it looks slightly different to the Apple example as they're both using NSComboBox!
Any guidance much appreciated!
uj5u.com熱心網友回復:
組合框初始化為
private let comboBox = NSComboBox(labelWithString: "")
但是init(labelWithString:)是NSTextField創建標簽的便利初始化程式。使用init()來代替:
private let comboBox = NSComboBox()
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/312580.html
標籤:macos cocoa nscombobox
