Getting started with Cocoa’s new auto layout

Auto layout might be the most exciting Mac only thing announced at WWDC this year. It improves on Cocoa’s idea of springs and struts and will let spend way less time building your UI.

Despite seeing auto layout used at WWDC, I couldn’t remember how to use it a couple months later when I sat down to play with it. Apple’s programming guide has lots of good stuff, but it seems to be missing a bit about getting started.

Default projects in Xcode 4.1 on 10.7 don’t default to using ¬†auto layout so you’ll need to enable it. (Note: Projects using auto layout are 10.7 only)

So now you can use auto layout in your project. How do you use auto layout? By adding constraints. You’ll add constraints from the editor menu.

You can also command+click on multiple controls and add constraints to more than one control at a time.

Here’s a short screencast of some simple things you can do with constraints. One great thing is that they work inside Xcode and you don’t need to build and run your app.

Leave a comment