by lyinsteve on 3/2/14, 8:45 PM with 72 comments
by bridger on 3/2/14, 10:11 PM
One advantage is that it makes sure things are pixel-integral, no matter the scale factor of the screen. For example, his code example,
button.x = self.view.width * 0.2; // Position the inset at 20% of the width.
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.
This is not as simple as just rounding everything, either! For example, if you have two views
[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.
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
button.x = self.view.width - self.view.width * 0.2 - button.width
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
|[spacerView][button]
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]|.
by RyanZAG on 3/2/14, 9:34 PM
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.
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.
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.
by agildehaus on 3/2/14, 10:31 PM
buttonView = [[UIButton alloc] init];
buttonView.translatesAutoresizingMaskIntoConstraints = NO;
[self.view addSubview:buttonView];
[self.view addConstraint:[NSLayoutConstraint constraintWithItem:buttonView attribute:NSLayoutAttributeTop relatedBy:NSLayoutRelationEqual toItem:self.view attribute:NSLayoutAttributeBottom multiplier:1.0f constant:0.0f]];
... etc
It's certainly better than the stress you'll have with UIView+Positioning or IB.
by accatyyc on 3/2/14, 9:41 PM
#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).
by SimianLogic2 on 3/2/14, 10:58 PM
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....
by vanwesson on 3/2/14, 10:03 PM
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).
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.
by fomojola on 3/3/14, 1:31 AM
by supercoder on 3/2/14, 10:45 PM
It's probably a testate to how good UIKit is to code with directly that makes it attractive.
Though whatever side you fall on, I think it's worth appreciating you have the choice. 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.
by mp3jeep01 on 3/2/14, 10:26 PM
by smallsharptools on 3/2/14, 9:18 PM
by k-mcgrady on 3/2/14, 10:34 PM
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.
by tejaswiy on 3/2/14, 9:34 PM
by pavlov on 3/3/14, 7:06 AM
I'm working on a design tool that aims to fix this: http://neonto.com
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.
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.
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!
by jfahrenkrug on 3/3/14, 1:55 PM
by chromejs10 on 3/3/14, 1:20 AM
by Zigurd on 3/3/14, 1:00 AM
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.
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?
by jarjoura on 3/3/14, 4:37 AM
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.
by fyolnish on 3/3/14, 7:27 AM
It's a take on Apple's visual format language that allows for defining a ui from scratch: https://github.com/fjolnir/Visual-Layout-Language/blob/maste...
by nteon on 3/2/14, 9:30 PM
by puppetmaster3 on 3/2/14, 9:47 PM
Designers don't do things in code, they use the UI panel designer. end of story.
by stcredzero on 3/3/14, 3:18 AM
by austinl on 3/2/14, 9:42 PM
I use UIView+Positioning, which exposes x, y, width,
height, right, bottom, centerX, and centerY as both
setters and getters.
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).by nicholassmith on 3/3/14, 1:24 PM
Plus on a 13" laptop on the move? Oof, hard to deal with. Only really could work with them easily on a larger monitor.
by ja27 on 3/3/14, 3:56 AM
by fecak on 3/2/14, 11:12 PM
by onmydesk on 3/3/14, 12:41 AM
You might need to use IB.
by blazespin on 3/2/14, 10:09 PM
by indubitably on 3/2/14, 10:28 PM