Until recently I’ve done an amazing job of avoiding using Auto Layouts on anything other than demo apps. All my apps within the App Store use the old layout approach of structs and constraints, which although being exceptionally easy to create, its limited when running across all the form factors that iOS runs on.
With multitasking on the iPad requiring Auto Layouts, I thought its probably about time that I took the time to learn to love the contraversal layout engine.
What is Auto Layout?
Auto Layout is a constraints based layout system for iOS, tvOS and OS X. It allows us to create adaptive user interaces that respond appropriatly to changes in screen size and orientation.
Auto Layouts is supported in both Xamarin Studio and Visual Studio when using Xamarin.iOS but will not be applicable to Xamarin.Forms developers.
Layout contraints
Contraints are a mathematical representation of the relationship between views. The NSLayoutContraint class is used to create contraints on both iOS and OS X but for the most part, you’ll want to create contraints using our iOS Designer rather than programatically.
Auto Layout has a number of constraints which include size, alignment and spacing. By providing each view within a scene with constraints, Auto Layout will determine the frame of each view at runtime.
One really important tip which will help you solve Auto Layout issues is to remember: Most controls will require constraints that define its height, width, x position and y position.
Getting Started
To get started, I want to center a UIView within my scene. I want to remain in the center of the scene no matter what size of device its running on.
Above you can see that I’ve added a UIView to my scene and set its background colour to blue. Although it looks like its center to my scene, no iOS device actually has this form factor. We can use the View As drop down to simulate the different size. Lets see how this would look on a 5s with no constraints.
We can see that my UIView isn’t position correctly! To resolve this, lets go ahead and add some contraints.
I’m going to lock its height and width. To do this, I’ll click the handle bar button.
This has now locked the width and height of my view so that no matter what device I’m running on, the box will always remain the same dimentions. You’ll note that the lines are currently orange. This means that the control contains some errors with its contraints.
If you recall back to my pro tip, you’ll know that we’re only 50% of the way to completing the contraints for this view. We have yet to provide Auto Layouts with any information on where to position this view.
To do this, I clicked on the orange button in the middle of the view and connect it to the verticle line running through the scene. I repeat this for the horizontal line. This is telling Auto Layout that I want this views X and Y to be centered on the center of the super view.
That the view now having 4 contraints (width, height, x and y), you’ll see the lines now turn to blue. This marks a valid set of contraints for the view!
Wrapping up
This is a crazy simple demo of Auto Layouts but provides you with the basic you need to get started.
Come back in a few weeks to see more Auto Layout goodness as I convert an existing login screen to use Auto Layout.