Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Constraint error for UIScrollView with an empty UIStackView in Interface Builder

I'm following the "Dynamic Stack View" part of this Stack View Apple tutorial, and I'm running into an constraint issue in Interface Builder if I leave the UIStackView (within the UIScrollView) empty, as opposed to putting a UIButton inside it. I want my UIViewController to dynamically create all of the arrangedSubviews, so I don't want to leave a single UIView in there that I will just have to handle or delete programmatically.

I can ignore the error and it will still work as I want, with no runtime constraint errors. However, I don't want to leave constraint issues within my xib/nib.

What's the cleanest way to satisfy Interface Builder? If I add a concrete height constraint, then I have to create an outlet for it and delete it programatically. That seems overly invasive and I'd rather leave as much Auto Layout in the xib/nib as possible.

The Interface Builder overview

The constraint error

like image 871
solidcell Avatar asked Sep 19 '16 07:09

solidcell


1 Answers

It turns out that in order to satisfy Interface Builder, the UIScrollView needs to have a height for its contents, and it won't assume a height of 0. So since the UIStackView is the content view, it needs to have a height. Even though at runtime it will have content, and thus a height, Interface Builder needs to have a fallback. So therefore, set the Intrinsic Size.

It's at the bottom of Size inspector:

enter image description here

Change Intrinsic Size from Default (System Defined) to Placeholder and set the Height to whatever you want. Something large enough to help while designing in Interface Builder is probably a good idea. You can leave Width set to whatever, or check the None checkbox.

like image 123
solidcell Avatar answered Nov 20 '22 20:11

solidcell