I experienced some autolayout issues while working with
My table view has a single column. It’s also the only subview of my
NSViewController’s view. Each
NSTableCellView should have two labels vertically centered, and horizontally in a line.
H:|-[firstLabel]-[secondLabel]-(>=default)-|. Simple layout. Should be easy.
The autolayout engine thought otherwise. There were unsatisfiable constraints. This was one of those strange cases where the autolayout was broken but visually, the layout was fine. But a warning is a warning, and my projects don’t have any room for warnings.
Unable to simultaneously satisfy constraints: "<NSLayoutConstraint:0x600000089d80 H:[NSTextField:0x600000183f60]-(>=NSSpace(20))-| (Names: id:0x600000183e90, '|':id:0x600000183e90 )>", "<NSLayoutConstraint:0x600000089f60 'NSView-Encapsulated-Layout-Width' H:[id(44)] (Names: id:0x600000183e90 )>", "<NSLayoutConstraint:0x600000089dd0 H:[NSTextField:0x600000184030]-(61)-[NSTextField:0x600000183f60]>", "<NSLayoutConstraint:0x600000089ce0 H:|-(5)-[NSTextField:0x600000184030] (Names: id:0x600000183e90, '|':id:0x600000183e90 )>"
The problem here is
NSView-Encapsulated-Layout-Width. Something wants to be a fixed width, but I don’t remember setting it. In fact, I didn’t set it (on purpose)
I took Xcode’s advice and set a launch argument of
-NSConstraintBasedLayoutVisualizeMutuallyExclusiveConstraints 1. Sure I could see that the constraint was broken, but it didn’t help me to understand the issue.
A cursory search for
NSView-Encapsulated-Layout-Width yielded some advice about setting one of my constraints affecting the horizontal layout to a priority of less than 1000. That solves the problem, but I don’t want to have a lower priority constraint. I want my constraints to be absolute.
Back in the storyboard, the
NSTableViewColumn was only 44 points wide with a minimum of 44 and a maximum of 1000. The problem was solved when I went back to the
NSTableView in storyboard and set the column’s current and minimum width to something large. It’s a single column table, so it’s OK to be wide.
That solved the problem. The constant for the
NSView-Encapsulated-Layout-Width constraint was 44 in the debugger–the same value as the table’s column in the storyboard. Setting the column’s width back to a small number caused the problem to appear again. Seems like a solid fix.
The moral of the story is that when you drag an NSTableView onto the storyboard, make sure to set the min max and current width of your table view columns.