I would still recommend using Auto Layout, even if you are laying out your views without Interface Builder. (Full disclosure, I helped write Auto Layout.)<p>One advantage is that it makes sure things are pixel-integral, no matter the scale factor of the screen. For example, his code example,<p>button.x = self.view.width * 0.2; // Position the inset at 20% of the width.<p>has a pretty good chance at starting the button at x=21.845, or some other point in-between pixels and producing a blurry line. If you do this same relation in Auto Layout, the engine makes sure that the positions and widths are all pixel-integral.<p>This is not as simple as just rounding everything, either! For example, if you have two views<p>[blue][red], you want to make sure you round their shared edge either to the left or to the right. Otherwise, there will be a gap between them. You need to make sure you round their shared edge the same way consistently too, or it will jump back and forth as you resize a window and produce a noticeable jitter.<p>Also, in that same code example, they set a button's x position to be directly related to the width of a view. If you ever want to support RTL interfaces, this is a bad idea. It is relating a width to a position. In a RTL interface, the correct code would be<p>button.x = self.view.width - self.view.width * 0.2 - button.width<p>Complicated! In general, you shouldn't convert between positions and sizes. Instead, I would make an invisible spacer view and lay them out like this using the layout format language<p>|[spacerView][button]<p>Then make a relation setting the spacerView's width to be 0.2 of the superview's width. This will produce constraints that correctly work in a RTL interface, laying it out like [button][spacerView]|.
There's generally two kinds of iOS apps/devs and once you work out what bracket you fall into, choosing between IB and code is easy.<p>If you're making fairly standard apps that substitute largely for websites - eg, login screens, some data in lists, couple forms - then you will probably want to go with IB storyboards. For these types of apps, development speed is most essential and future changes are mostly just tweaking the UI a bit as a large part of the functionality of the app is just pulling data from web services or doing standard calculations and displaying the result. Storyboards will let you get a nice looking and fairly simple UI done very fast and allow for rapid UI changes.<p>However, if you are going to have 3+ devs working on your app because it's actually the basis for a business or is very complicated, storyboards cause a lot of merge problems. They're far harder to create automated test code for. Refactoring your app becomes an exercise in tracking down IBOutlets. The couple days you saved at the start with easy layout and transitions get eclipsed by the amount of time you spend fighting IB later for changes. Also, if you use code review tools and have a heavy peer review culture then storyboards are a particularly bad fit.<p>Honestly I believe most apps fit into the first option and storyboards are the way to go. 95%+ of the apps on the Apple Store are definitely in the first category, and there usually isn't a need to over engineer them.
Why not just use Auto Layout in pure code?<p>buttonView = [[UIButton alloc] init];<p>buttonView.translatesAutoresizingMaskIntoConstraints = NO;<p>[self.view addSubview:buttonView];<p>[self.view addConstraint:[NSLayoutConstraint constraintWithItem:buttonView attribute:NSLayoutAttributeTop relatedBy:NSLayoutRelationEqual toItem:self.view attribute:NSLayoutAttributeBottom multiplier:1.0f constant:0.0f]];<p>... etc<p>It's certainly better than the stress you'll have with UIView+Positioning or IB.
I have some problems with this post.<p>#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!<p>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).<p>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.<p>Yes, it is harder to get started with auto layout, but once you learn it, it is worth it.<p>#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).
If all of your layouts are created in code, every visual tweak requires an engineer. In general I will do almost anything to NOT have to lay out views by hand, as nothing slows a project down more than jumping into the tweak-a-magic-number-and-recompile cycle. It sucked when I was doing CSS, and it sucks even more when I'm compiling code.<p>For the most part IB mostly doesn't make sense for games, so I have a custom Photoshop script that exports each layer with metadata. A custom importer reads the metadata file and loads the whole view in the proper positioning. So I've basically swapped IB for Photoshop, but the work flow is essentially the same....
> I use UIView+Positioning...<p>Looking at that code reveals that it doesn't try to account for fractional positioning. If you set self.center on a view that is an even number of pixels wide, it will result in a non-pixel-aligned origin on non-retina displays, which results in in blurry rendering. If you're iOS-7 and iPhone only these days, that may not matter, but there are still millions of non-retina iPads out there, and in fact you can still buy new ones on the Apple store (both the original iPad mini and the iPad 2 are still for sale).<p>I've written lots of code for iOS and have generally always preferred programmatic layout over IB, but you have to be a little more careful than this post is implying. Beefing up your helper routines to take into account issues like the above is critical to making sure your UIs always look their best.
As someone who has done a decent amount of Swing/Android development, I've often wondered: why hasn't iOS historically had decent layout manager support? I've used RelativeLayout for Android and quite frankly I've found very few instances where it didn't do what I wanted: the reduced number of iOS form factors makes it easier to do pixel-perfect layouts (than the thousands of Android devices) but even AutoLayout seems like a poor replica of what the Swing/Android layout manager-style libraries can do. I use it both from the Android XML and from code and it makes life REALLY easy. I've done a lot of work with storyboard/IB recently and it isn't as bad as I used to think, but always wondered about that.
I definitely side with the author. Any time we've used IB it's always ended up in regret. Refactoring, odd layout issues, iPad / iPhone management has always made it a headache.<p>It's probably a testate to how good UIKit is to code with directly that makes it attractive.<p>Though whatever side you fall on, I think it's worth appreciating you have the <i>choice</i>. I remember when doing some brief Windows Phone development, and seemed all you had to work with was some XML API to design your interfaces. It was awful.
One of the bigger takeaways from this article should be to make a decision of where to set layout parameters, and stick with it. I'm sure we all have opinions on using the in-code method vs IB, and can debate 'till the end of time. As a relatively new iOS dev picking up an existing codebase, the biggest thing I could say is be consistent. The number of times I tried setting something in code only to find it was being changed in IB is far too many.
No thanks. Just learn to Storyboards already. It is it that hard and Xcode can help anyone who maintains this app. If it all in code it takes a lot of the and effort to understand what is controlling the layout.
I think this is all pretty valid. However it's important to point out the time savings using NIB's and Storyboards. I do most of my UIs in code but occasionally use NIB's when I'm doing layouts that include lots of objects that would be very time consuming to code.<p>Recently I decided to give Storyboards a try on a project. I was shocked at how much time it saved (no more pushing/presenting view controllers, no more fighting tables to display custom cells) and for views that were only displaying information and a button to push another controller I didn't even need to create a class as the push could be done in Interface Builder. I don't think this approach is going to work well for all apps but when you've got something with a predetermined flow (a form for example) it saves a hell of a lot of time.
I see where this is coming from, but loosing the convinience of xibs + auto layout is too great to sacrifice for me. Just thinking of how much bigger my view controller code will get makes me shudder.
The big problem with Interface Builder is that, most of the time, its layout view bears no resemblance to what your UI actually looks like within the app. Deciphering the meaning of overlapping grey boxes in IB is often just as difficult as reading code. "WYSIWYG" it ain't...<p>I'm working on a design tool that aims to fix this:
<a href="http://neonto.com" rel="nofollow">http://neonto.com</a><p>Neonto Studio is a completely visual environment with support for vector drawing, video, smart guides, multi-device layout with full previews, and so on... There's no coding involved -- this is an app for designers.<p>The tool generates readable iOS and Android code, and there's no special runtime involved, so it's ridiculously easy to take a few screens designed in Neonto Studio and drop them into a larger app.<p>It's currently in alpha testing. I'm hoping to release a full beta in the next few months. If you're interested in trying out the alpha today, I'd love to have your help -- just drop me an email at pauli <at> neonto dot com!
Don't make the mistake of thinking "Storyboard == Auto Layout". I really like Auto Layout and I think it is much easier to understand and read when declared in code. I strongly dislike Storyboards, though, for the 15 reasons outlined here: <a href="http://stackoverflow.com/questions/9404471/when-to-use-storyboard-and-when-to-use-xibs/19457257#19457257" rel="nofollow">http://stackoverflow.com/questions/9404471/when-to-use-story...</a>
However, some pains can be removed with this tool I wrote: <a href="https://github.com/jfahrenkrug/StoryboardLint" rel="nofollow">https://github.com/jfahrenkrug/StoryboardLint</a>
It helps you to keep your IDs in code and in your Storyboards in sync.
The way I've typically worked is static pages are allowed to be done in IB, but everything else is done in code. The reasoning is that doing things in code makes for much easier diffs and merges and also allows you to easily edit code in things outside of XCode (I personally prefer AppCode). Doing things in code makes thing a lot more explicit and can be setup so that the view code doesn't have to interfere and floor the view controller's code. The main problem is there are lots of things you can do in code that IB can't do, but not much (if anything) the other way around. You should always be able to write your views using pure code.
From an Android PoV I'm very skeptical of the benefits this. Unless you can round-trip between code and a visual tool, you are unlikely to get professional designers to touch a UI layout in code.<p>It's hugely valuable to get designers to adopt the SDK's design tools. Unless the designers on a project are obstinate about that, making that more difficult is usually a step in the wrong direction.<p>He also seems to be blaming the toolchain's refactoring and the declarative UI XML for being difficult to manage. It is really that bad?
Writing a large scale iOS app with 100s of XIB based views is equivalent to writing a Visual Basic app in my opinion. Suddenly your app is now dependent on -awakeFromNib and subclassing requires yet another another XIB. Just some reasons I prefer writing layouts in code (auto or not).<p>There are two camps of iOS Engineers in my experience. Those that feel IB makes your life better and those that feel the opposite. Both sides make valid points, but I vote for sticking with code as much as possible.
Related project that I'm working on: <a href="https://github.com/fjolnir/Visual-Layout-Language" rel="nofollow">https://github.com/fjolnir/Visual-Layout-Language</a><p>It's a take on Apple's visual format language that allows for defining a ui from scratch: <a href="https://github.com/fjolnir/Visual-Layout-Language/blob/master/Mac%20Demo/test.vll" rel="nofollow">https://github.com/fjolnir/Visual-Layout-Language/blob/maste...</a>
I do this too, and agree. The benefits and straightforward version control diffs are fantastic when working on a distributed team with multiple devs touching the UI.
Same issue in Android. But this does not work in real life, because Designers.<p>Designers don't do things in code, they use the UI panel designer. end of story.
Using Tcl/Tk, one did layout interactively at the command line. It was fast and very intuitive, especially in the way it used layout guides with easily understandable local "laws of physics." However, it usually resulted in very staid looking UIs. Also, such a scheme would have to be modified to be able to deal gracefully with things like device rotation.
<p><pre><code> I use UIView+Positioning, which exposes x, y, width,
height, right, bottom, centerX, and centerY as both
setters and getters.
</code></pre>
IB/code aside, this is convenient. All of those properties are read-only by default, and that's always seemed counterintuitive to me (though I'm sure there's some purpose I'm missing).
I built an app using Storyboard last year, I'd do it again but only if the app was reasonably simple in scope. I think we ended up with about 20+ screens, and at that stage there was too much going on for it to be easy to work with.<p>Plus on a 13" laptop on the move? Oof, hard to deal with. Only really could work with them easily on a larger monitor.
When I first saw Interface Builder 25 years ago, I thought it was a neat tool for the programming-impaired. Maybe it's finally getting useful but even with the latest iOS autolayout, I still prefer doing my own layout calculations in code most of the time.
As an aside, I think it's pretty impressive that a post by someone who graduated high school 8 months ago is being discussed and debated on the top of HN. He says he is looking for an internship, hopefully he will get some interest. Kudos.
It looks to me like ios7 and auto layout is partly about running on many screen sizes. Makes you think about future devices and how your code will fare. Worth thinking about.<p>You might <i>need</i> to use IB.