Search icon CANCEL
Subscription
0
Cart icon
Your Cart (0 item)
Close icon
You have no products in your basket yet
Arrow left icon
Explore Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Conferences
Free Learning
Arrow right icon

Evenly Spaced Views with Auto Layout in iOS

Save for later
  • 5 min read
  • 16 Apr 2015

article-image

When the iPhone first came out there was only one screen size to worry about 320, 480. Then the Retina screen was introduced doubling the screen's resolution. Apple quickly introduced the iPhone 5 and added an extra 88 points to the bottom. With the most recent line of iPhones two more sizes were added to the mix. Before even mentioning the iPad line that is already five different combinations of heights and widths to account for.

To help remedy this growing number of sizes and resolutions Apple introduced Auto Layout with iOS 6. Auto Layout is a dynamic way of laying out views with constraints and rules to let the content fit on multiple screen sizes; think "responsive" for mobile. Lots of layouts are possible with Auto Layout but some require an extra bit of work.

One of the more common, albeit tricky, arrangements is to have evenly spaced elements. Having the view scale up to different resolutions and look great on all devices isn't hard and can be done in both Interface Builder or manually in code. Let's walk through how to evenly space views with Auto Layout using Xcode's Interface Builder.

Using Interface Builder

The easiest way to play around and test layout in IB is to create a new Single View Application iOS project.

 evenly-spaced-views-auto-layout-ios-img-0

Open Main.storyboard and select ViewController on the left. Don't worry that it is showing a square view since we will be laying everything out dynamically.

evenly-spaced-views-auto-layout-ios-img-1

The first addition to the view will be the three `UIView`s we will be evenly spacing. Add them along the view from left to right and assign different colors to each. This will make it easier to distinguish them later. Don't worry about where they are we will fix the layout soon enough.

 evenly-spaced-views-auto-layout-ios-img-2

Spacer View Layout

Ideally we would be able to add constraints that evenly space these out directly. Unfortunately, you can not set *equals* restrictions on constraints, only on views. What that means is we have to create spacer views in between our content and then set equal constraints on those. Add four more views, one between the edges and the content views.

 evenly-spaced-views-auto-layout-ios-img-3

Before we add our first constraint let's name each view so we can have a little more context when adding their attributes. One of the most frustrating things when working with Auto Layout is seeing the little red arrow telling you something is wrong. Let's try and incrementally add constraints and get back to a working state as quickly as possible. The first item we want to add will constrain the Left Content view using the spacer. Select the Left Spacer and add left 20, top 20, and bottom 20 constraints.

 evenly-spaced-views-auto-layout-ios-img-4

To fix this first error we need to assign a width to the spacer. While we will be removing it later it makes sense to always have a clean slate when moving on to another view. Add in a width (50) constraint and let IB automatically and update its frame.

 evenly-spaced-views-auto-layout-ios-img-5

evenly-spaced-views-auto-layout-ios-img-6

Now do the same thing to the Right Spacer.

 evenly-spaced-views-auto-layout-ios-img-7

evenly-spaced-views-auto-layout-ios-img-8

Content View Layout

We will remove the width constraints when everything else is working correctly. Consider them temporary placeholders for now. Next lets lay out the Left Content view. Add a left 0, top 20, bottom 20, width 20 constraint to it.

 evenly-spaced-views-auto-layout-ios-img-9

evenly-spaced-views-auto-layout-ios-img-10

Follow the same method on the Right Content view.

 evenly-spaced-views-auto-layout-ios-img-11

Unlock access to the largest independent learning library in Tech for FREE!
Get unlimited access to 7500+ expert-authored eBooks and video courses covering every tech area you can think of.
Renews at €18.99/month. Cancel anytime

evenly-spaced-views-auto-layout-ios-img-12

Twice more follow the same procedure for the Middle Spacer Views giving them left/right 0, top 20, bottom 20, width 50 constraints.

 evenly-spaced-views-auto-layout-ios-img-13

evenly-spaced-views-auto-layout-ios-img-14

evenly-spaced-views-auto-layout-ios-img-15

evenly-spaced-views-auto-layout-ios-img-16

Finally, let's constrain the Middle Content view. Add left 0, top 20, right 0, bottom 20 constraints to it and lay it out.

 evenly-spaced-views-auto-layout-ios-img-17

evenly-spaced-views-auto-layout-ios-img-18

Final Constraints

Remember when I said it was tricky? Maybe a better way to describe this process is long and tedious. All of the setup we have done so far was to set us up in a good position to give the constraints we actually want. If you look at the view it doesn't look very special and it won't even resize the right way yet. To start fix this we bring in the magic constraint of this entire example, Equal Widths on the spacer views.

Go ahead and delete the four explicit Width constraints on the spacer views and add an Equal Width constraint to each. Select them all at the same time then add the constraint so they work off of each other.

evenly-spaced-views-auto-layout-ios-img-19

 evenly-spaced-views-auto-layout-ios-img-20

Finally, set explicit widths on the three content views. This is where you can start customizing the layout to have it look the way you want. For my view I want the three views to be 75 points wide so I removed all of the Width constraints and added them back in for each.

Now set the background color of the four spacer views to clear and hide them. Running the app on different size simulators will produce the same result: the three content views remain the same width and stay evenly spaced out along the screen. Even when you rotate the device the views remain spaced out correctly.

 evenly-spaced-views-auto-layout-ios-img-21

evenly-spaced-views-auto-layout-ios-img-22

evenly-spaced-views-auto-layout-ios-img-23

evenly-spaced-views-auto-layout-ios-img-24

Try playing around with different explicit widths of the content views. The same technique can be used to create very dynamic layouts for a variety of applications. For example, this procedurecan be used to create a table cell with an image on the left, text in the middle, and a button on the right. Or it can make one row in a calculator that sizes to fit the screen of the device. What are you using it for?

About the author

Joe Masilotti is a test-driven iOS developer living in Brooklyn, NY. He contributes to open-source testing tools on GitHub and talks about development, cooking, and craft beer on Twitter.