Road Test: PureLayout

Imagine there’s no translatesAutoresizingMaskIntoConstraints. It’s easy if you try. No constraintWithItem:attribute:relatedBy:toItem:attribute:multiplier:constant: below us, above us only sky. Imagine your app not crashing because you left a “]” out of constraintsWithVisualFormat, woohoo.

*Cough* Sorry about that, folks. Let’s move on.

I’ve written before about the superiority of programmatically doing your layout code, but there is no denying that the tools for doing so are kinda kludgy. AutoLayout is powerful and flexible but we are in need – in need! – of some syntactic sugar of some kind.

You could write a lot of helper methods for this purpose, or you could just use PureLayout.

PureLayout is an open source library that makes AutoLayout a lot less painful to deal with. Say goodbye to manually setting translatesAutoresizingMaskIntoConstraints for your views, local view defines because you can’t have self.aButton in your view dictionaries, even [view addConstraints] – in short, if it’s in AutoLayout and it’s annoying, PureLayout takes care of it. You’ll do the same work in half the code.

PureLayout example

Write layout code like a frikking wizard! (From PureLayout example project)

Best of all, that’s all PureLayout does; unlike some open source libraries, it doesn’t try and do too much. If you already know AutoLayout, the learning curve is about 5 minutes.

Are there any disadvantages to PureLayout? For very complex layouts, the ability to produce 17 constraints at once with constraintsWithVisualFormat might be preferable. Other than that, I can’t see any reason why you shouldn’t use PureLayout for all your layout needs.

Posted in Uncategorized | Tagged , , , | Leave a comment

How to animate UISearchBar like in Contacts

Ever seen the animation that occurs when you tap in the search bar in Contacts? The navigation bar slides upwards, the search bar goes up and replaces it, and the Cancel button comes in from the left.

Here’s a before and after:

before animation after animation

Now, how is that done?

The first question is how to animate the Navigation Bar out of the way. This can be done with a simple frame animation as follows:

CGRect frame = self.navigationController.view.frame;
frame.origin.y -= navbarHeight;
frame.size.height += navbarHeight;
[UIView animateWithDuration:duration animations:^(){
self.navigationController.view.frame = frame;

This also moves your entire view controller up, and the search bar with it.

Second step: handling the cancel button. The UISearchBar has a showsCancelButton, but if you try and animate that it comes off kinda clunky. The solution is to have the Cancel button on at all times – but set the search bar to be so wide that the cancel button is off the screen to the right. After this, a simple frame or constraint animation will get the job done.

self.searchWidth.constant = 320.f;
[self.searchBar setNeedsUpdateConstraints];
[UIView animateWithDuration:duration animations:^(){
[self.searchBar layoutIfNeeded];

Naturally you will want to combine this with the Navigation Bar example :/

Next, you will probably find that the status bar appears on top of the search bar. My solution to this was to create a spacer view to appear between the navigation bar and the search bar, and adjust the height via a constraint animation. The code for this is left as an exercise for the reader.

Have fun animating, and if you have any tips or tricks to share, let me know in the comments!

Posted in Uncategorized | Tagged , , | Leave a comment

Control a fridge-opening robot via an iOS app

While at the supermarket today, I couldn’t remember whether we needed to buy meat or not. Suddenly the thought hit me – what if I had a robot that could open the fridge door for me so I could look, and I could control it remotely via an iOS app? You can hook up webcams and view them remotely and I suppose you could put a webcam in the fridge. There is only one downside to this approach: a robot with camera is much cooler than a webcam. All of which is a long-winded introduction to this: a navigable robot controlled via an iPad. Sweet.

If you did actually want to build a fridge-opening robot with an iOS interface, one way to go about it is by using an Arduino motherboard. There is a tutorial at SparkFun showing just how to do that. It involves buying an iOS app rather than writing your own, but still.

Here is a video demonstrating a fridge-opening robot with an Arduino motherboard:

Posted in Uncategorized | Tagged , , | Leave a comment

AutoLayout: Interface Builder vs code

I haven’t blogged here for 10 years but now I return … to blog mainly about iOS programming! Or so the plan is.

I’ve been an iOS developer on and off for about 5 years now and when AutoLayout first appeared I avoided it as long as I could. My first memory of trying AutoLayout with IB is getting quickly frustrated trying to add constraints that didn’t do what I wanted and the default ones doing the opposite of what I wanted. So back to code it was.

Time has passed and I’ve become not quite an expert in AutoLayout but certainly proficient in it. Every so often I read a blog post or article about the wonders of IB + AutoLayout and how it halves your time creating layouts, gives you flexibility, cooks dinner for you, does the dishes afterwards and so on. Finally I found a bit of time to kill but with no one to kill at hand, I thought I’d give the IB side of things another go.

Everyone agrees there are downsides to IB, for example with source control and merging, but do the pros outweigh the cons?

In short, no. Why? Read on dear sir and all will be revealed!

There are a number of strange quirks in the IB interface that make things unintuitive, such as:

– The first thing you notice is that all the constraints get lumped together in a haphazard order making them hard to read.

– You can click a view to reveal only the constraints applying to it but you can’t delete them here; it is necessary to do this in the massive clumped list noted prior.

– By default views don’t have meaningful labels. In my view, once they are connected the label in IB should match the property name. Instead you have to manually name everything twice.

Views named view

Views named view – that’s intuitive!

– For some reason the Pin button doesn’t allow you to pin to a superview; you must use the menus for that. Why?

Another problem is conceptual: in code, you soon learn to ignore frames entirely when constructing layouts. In IB however, it tries to honour the initial frames you place your views in, and defaults the constants in your constraints to suit. This seems bizarre to me. I thought the idea of AutoLayout was to not concern yourself with frames?

But for me, the real IB killer is building constraints with visual format language. If you want to have several views in a vertical list, in code you can do so with one formatting string:


and done!

In IB you manually have to create each constraint and then edit each of them because again the Pin button doesn’t provide enough options.

For a layout of any complexity I’d bet on coding to outrace IB hands down.

I know some prefer IB because there’s less code, but come on: we are programmers. Afraid of code we should not be.

So what do you think? Is coding layouts better or is there a magic feature in IB that I’ve overlooked?

Posted in Uncategorized | Tagged , , , | 2 Comments

*** STOP PRESS *** My Opinions Are Important moves to its own domain!

That’s right people – this blog is moving to its own domain and its own space. Check it out!

The new address is

The feed address is now

Please update your links/bookmarks/feed readers accordingly.

All the posts and comments have been ported across, so you can read them all again – and again – and again!

See you all over there.

STRANGENESS UPDATE: Hmmm, apparently new blog now thinks its feed address is Why did it change? Not sure, but if you have problems with the feed=rss2 address, try the other one instead.

Posted in Important Announcements | 2 Comments

Great gifts for geeks

TheMolk found some excellent off-the-wall gift ideas at ThinkGeek, including a tiny remote-controlled helicopter and a tiny, yet fully functional Japanese battle tank.


A great gift idea, and oh-so-handy next time World War III erupts in your home or workplace.

Posted in Interesting, but otherwise unclassifiable, Nutbar | 2 Comments

Wedding gifts for the strange: spider bowl

From Laura Zindel Ceramics, as featured on bioephemera:


You are probably asking yourself why. Zindel’s explanation:

I hope that I can make art that a family member can buy to be handed down the line. Something bought on a whim, that becomes the platter for the turkey, or sits on the mantel. “Crazy old Uncle Larry bought that peculiar spider platter, and we just can’t seem to part with it”, I would like to be apart of that.

There’s also beetles, snakes and cicadas, oh my.

Posted in Nutbar | 7 Comments