0

I am trying to add a few NSTextFields to a horizontal stack view programmatically but the StackView only ever has one field visible and the field is not arranged correctly.

for item in ArrayOfStrings {
    let view = NSTextField()
    view.stringValue = item
    stackView.addView(view, in: .leading)
}

I have tried the following with the same result

stackView.addArrangedSubview(view)

It seems to work fine in Interface Builder - adding additional subviews results in it automatically showing them and adjusting their size.

Duncan Groenewald
  • 8,496
  • 6
  • 41
  • 76

1 Answers1

0

Not sure this isn't just a bug but it seems that by simply adding a single constraint to the NSTextField things start working.

for item in shelf.activeItems {
            let view = ShelfItemTextField(frame: NSRect.zero)
            view.widthAnchor.constraint(greaterThanOrEqualToConstant: 20.0).isActive = true
            view.stringValue = item.product?.code ?? "-"
            stackView.addView(view, in: .leading)
            if item.isNew {
                view.backgroundColor = Theme.Colors.newProduct
                view.drawsBackground  = true
            }
        }
Duncan Groenewald
  • 8,496
  • 6
  • 41
  • 76