Auto Layout and Percentages

Maybe I’m just crazy, but it feels like Auto Layout kind of hates me. If you don’t feel that way, try doing making a UIView that is 50% height and 100% width of the container.

In a Storyboard, it’s actually not too hard—a little roundabout, but not bad once you know about multipliers. Create the UIView, Ctrl-drag from the UIView to the container view and select “Equal Height.” You can then edit properties of the constraint and set the Multiplier to 1:2 (this is what makes it 50% height). Add your Top, Left and Right constraints to make it 100% width and you’re good.

I had a XIB though.

There didn’t seem to be any way to create an “Equal Height” relationship with the root view object and the UIView I had placed. I ended up having to create a separate UIView that I made 100% width and height of its container (Top, Left, Right and Bottom constraints set to 0) to use as a placeholder for my Equal Height relationship with my UIView. Once I connected those two, I followed the same process of changing the multiplier to 1:2 and adding the Top, Left and Right constraints; the sizing worked like a charm at that point.

The fact that I had to “trick” Auto Layout is a little unnerving.

Now, there has been tremendous progress with Auto Layout in the last couple of years. And it’s way better than the old springs and struts method. The fact that I was doing all this in a view intended for both and iPhone and iPad was awesome. However, it still never really feels like it “just works.” Maybe I’m just asking too much though?