My question is a duplicate of this question asked 4 years ago, but it was never answered.
I have an NSSplitView with two subviews. The right subview has a label I want to center horizontally and vertically. The basic skeleton looks like this:
let window = /* ... */
let blankView = NSView(frame: .zero)
let customView = NSView(frame: .zero)
// set background colors
let title = NSTextField(labelWithString: "Title")
customView.addSubview(title)
let splitView = NSSplitView(frame: .zero)
splitView.isVertical = true
splitView.addSubview(blankView)
splitView.addSubview(customView)
window.contentView = splitView
Then I added in the Auto Layout code:
customView.translatesAutoresizingMaskIntoConstraints = false
NSLayoutConstraint.activate([
title.centerXAnchor.constraint(equalTo: customView.centerXAnchor),
title.centerYAnchor.constraint(equalTo: customView.centerYAnchor),
])
but now the slider can't be adjusted and the window can only be resized horizontally. The right view remains fixed in size.
I've tried various combinations of translatesAutoresizingMaskIntoConstraints =
, setContentHuggingPriority(:for:)
and setHoldingPriority(:forSubviewAt:)
but nothing works.
I can recreate this view in Interface Builder with no problems, but I don't know how to do it programmatically.