Search code examples
macosautolayoutinterface-builder

Why does this simple autolayout collapse in IB?


I have a window with a textview in a scrollview and two buttons. I have added constraints at all sides, leading, trailing, top, and bottom. For the buttons I have fixed width and height, and distance to bottom and edge.

I want it to look something like this:

Ideal window in Interface Builder

And this is what Interface Builder keeps giving me:

Broken window layout in Interface Builder

It also keeps offering to fix ambiguities by adding missing constraints, but actually clicking the button has no effect. No constraints are added. The error (and the offer to fix it) remains.

Interface Builder error message

I've tried added the constraints it is asking for, although I can't see why they would be needed and at least the buttons already have (fixed) width constraints. But I keep getting errors and the window keeps getting shrunk to nothing.

When actually running the program, the window looks fine. But I suspect these autolayout errors have something to do with this problem: NSTextView in magnified NSScrollView breaks on resize

The project is here: https://github.com/angstsmurf/spatterlight/tree/helpviewtest

(The branch helpviewtest is a cut down test case created specifically for this problem.)


Solution

  • You have a Text view embedded in a Clip view embedded in a Scroll view, none of which have an intrinsic content size.

    IB has no idea what's going to go on at run-time.

    So, while everything looks great when you run this, your code will have supplied enough information to lay things out as desired.

    To get it to "look right" in IB (and avoid the errors/warnings), you can give the ScrollView a Placeholder Intrinsic Content Size:

    enter image description here