0

I'm trying to layout an NSComboBox in a Mac app and I'm getting some weird behaviour. In reality I've got a view which has an NSComboBox as a subview along with some other subviews, but I've created a simple example to display the same issue I'm seeing.

For some reason, the text field isn't exactly vertically centered like I'd expect.

Here's my TestComboBox: NSComboBox with text slightly misalligned

Here's an Apple NSComboBox (note the text is vertically centered and there's a small amount of leading spacing before the highlight colour):

An example of Apple's NSComboBox

I've created a simple example to show the issue:

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

If you add the above view to a basic Mac app (Storyboard) and pin it to the view controller with the following constraints:

The constraints to set on the view to reproduce issue

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!

ADB
  • 591
  • 7
  • 21
  • What do you mean by "the text field doesn't stick to the centre of the combo box", which text field? – Willeke Oct 09 '21 at 08:40
  • As in I'd expect the text within the combo box to be perfectly vertically centered, whereas it seems to be higher up for some reason. I've edited my question and added another image for comparison. – ADB Oct 09 '21 at 10:52
  • Does this answer your question? [Vertically aligning text in an NSTextField using Swift](https://stackoverflow.com/questions/34379353/vertically-aligning-text-in-an-nstextfield-using-swift) – Willeke Oct 09 '21 at 11:35
  • Or [How do I vertically center align text in a NSTextField?](https://stackoverflow.com/questions/8624834/how-do-i-vertically-center-align-text-in-a-nstextfield) – Willeke Oct 09 '21 at 11:36
  • If you use a storyboard, why don't you add the `NSComboBox` directly to the storyboard, without all this code? – Ely Oct 09 '21 at 13:10
  • Because I'm not actually using storyboards - the combo box is just part of another element and I'm laying out within `layout()` on `NSView`. But this is just a reproducible example of what I'm seeing. – ADB Oct 09 '21 at 13:16
  • How do you vertically enter a text field? Or do you mean "the text isn't exactly vertically centered"? – Willeke Oct 10 '21 at 07:53
  • Have you tried `comboBox.translatesAutoresizingMaskIntoConstraints = false` before `addSubview`? – Willeke Oct 10 '21 at 07:57
  • @Willeke Not sure what you mean re vertically entering a text field. I just mean it doesn't look right (see comparison of my combo box to Apple's) - the text is just in the wrong place. And yes, I tried translatesAutoresizingMaskIntoConstraints but no luck unfortunately! Thanks – ADB Oct 10 '21 at 11:01
  • In the question: "the text field isn't exactly vertically entered" and "the text is vertically entered". – Willeke Oct 10 '21 at 11:43
  • `init(labelWithString:)` is a convenience initializer of `NSTextField` to create a label. Use `init()` instead: `NSComboBox()`. – Willeke Oct 10 '21 at 11:52
  • Apologies - when I edited my answer I knocked the 'c' off of 'centred'. Thanks - I've just tried the different init (`init()`) and that was exactly the problem! If you want to answer with that I can accept your answer. Thanks for your help. – ADB Oct 11 '21 at 05:14

1 Answers1

0

The combo box is initialized with

private let comboBox = NSComboBox(labelWithString: "")

but init(labelWithString:) is a convenience initializer of NSTextField to create a label. Use init() instead:

private let comboBox = NSComboBox()
Willeke
  • 14,578
  • 4
  • 19
  • 47