Skip to content

Comment on Laying Out iOS UIs in Code

Comments

I have some problems with this post.

#1 is saying that you're restricted to point based UI's in interface builder. This is absolutely not true. Wether you do you UI's in interface builder or code - use auto layout!

This way you get dynamic, resolution-independent UI's that can even work automatically with right-to-left text (realigning other elements according to text alignment).

The way the author is doing it is still point-based (just a bit more dynamic since he does some calculations) while auto layout has all this built in.

Yes, it is harder to get started with auto layout, but once you learn it, it is worth it.

#2, using things like UIView+Positioning still sets your frames. And it does so for each property you set. This means that the frame might be set 5 times instead of one (just for slightly cleaner code). This is not good since it might cause 5 calls to layoutSubviews instead of one (performance issue) and also it will ruin animations (dependent on a source and target frame).

Regarding layoutSubviews, that's incorrect. Adjusting the frame will call setNeedsLayout, multiple calls of which will be coalesced to a single layoutSubviews by CoreAnimation's runloop observer.

Pretty much all of my experiences with auto layout in code have taken longer both in code length and in time than just implementing -layoutSubviews. Explicit is better than implicit, etc.

Eventually you will get a handle on AutoLayout and it will become much easier. I learned to follow a few patterns that I taught myself which are solid for my interfaces. If I did it all in code I would be shooting myself and any other developer who has to touch this project later.

AboutSource Built by g1lg1l

Hackerly is an independent reader for Hacker News, built on the public HN API. Not affiliated with Y Combinator.