Tag: Auto Layout

iOS Auto Layout in practice

I’ve made a simple presentation at Tokyo iOS Meetup in Dec. 2012, which covers some basic ideas about utilizing the AutoLayout in iOS application. I found that about 1/3 developers there had already adopted AutoLayout in their projects. As I thought most people would stick to the classic “Spring and Struts” layout system then, the result was surprising to me.

In these slides, the basic idea I want to show is that try to make the layout system work as you want in the very beginning rather than to solve the problems when you see the UI acts oddly.

To achieve this, there is a simple rule called the “Rule of 4”, which means you need just 4 constraints (either created by the IB or yourself) on each view, and you can get the layout system work properly. No matter how powerful the AutoLayout API is, just place constraints as less as possible.

The “Rule of 4” will help you eliminate the ambiguous constraints or unsatisfied layout in most cases.

I’ve shown some common patterns about applying the “Rule of 4”. And I hope these patterns might be useful.

As to which tool do I suggest to use when dealing with layout constraints, I think it quite hard to give an answer. Although I’m a heavy user of IB and IB gives more visible hints, IB is not intelligent enough yet to understand your intention on layout, more over, IB is not literally equal to the API.

Update: Ole has written a blog titled 10 Things You Need To Know About Cocoa Autolayout, which is also a great summary of Cocoa Auto Layout.