Free Trial
|

Tip: Unsatisfiable Auto Layouts in iOS 6

In our previous two articles on Auto Layout in iOS 6, (Flexible Auto Layout and Layout Constraints) we explored how to build a simple tab bar layout with NSLayoutConstraints. What we didn’t see is how to debug issues with these kinds of layouts.

Unsatisfiable layouts

If you specify constraints that cannot be satisfied simultaneously, you’ve created a paradox for the poor Auto Layout system. Generally this will cause an exception immediately upon adding a constraint that cannot be satisfied.

Unlike most Objective-C exceptions, Auto Layout exceptions are accompanied by a verbose console spew. When you see something like this, don’t panic:

2012-09-30 13:24:33.614 AutolayoutExample[49263:c07] Unable to simultaneously
satisfy constraints.
Probably at least one of the constraints in the following list is one you don't
want. Try this: (1) look at each constraint and try to figure out which you don't
expect; (2) find the code that added the unwanted constraint or constraints and
fix it. (Note: If you're seeing NSAutoresizingMaskLayoutConstraints that you don't
understand, refer to the documentation for the UIView property
translatesAutoresizingMaskIntoConstraints)
(
"<NSLayoutConstraint:0x7464050 H:[UIView:0x7460e60]-(0)-| (Names: '|':UIView:0x745f950 )>",
"<NSLayoutConstraint:0x7463fa0 H:[UIView:0x7460d80]-(NSSpace(8))-[UIView:0x7460e60]>",
"<NSLayoutConstraint:0x74638f0 H:[UIView:0x7460d80]-(0)-| (Names: '|':UIView:0x745f950 )>"
)
Will attempt to recover by breaking constraint
<NSLayoutConstraint:0x757cc60 H:[UIView:0x7184bb0]-(NSSpace(8))-[UIView:0x7184c90]>
Break on objc_exception_throw to catch this in the debugger.
The methods in the UIConstraintBasedLayoutDebugging category on UIView listed
in <UIKit/UIView.h> may also be helpful.

The above simply means there are conflicting constraints that cannot be satisfied simultaneously. Usually it’s just a logic error on your constraints, and the debug output is enough to determine which ones are the problem.

To learn more about Auto Layout constraints take a look at Erica Sadun’s The iOS 6 Developer’s Cookbook, which contains all of the essential recipes you need to quickly start building successful iOS apps for iPhone, iPad, and iPod touch.

NSAutoresizingMaskLayoutConstraints

First off, there is always a sentence in the debug spew that says “If you’re seeing NSAutoresizingMaskLayoutConstraints that you don’t understand, refer to the documentation for the UIView property translatesAutoresizingMaskIntoConstraints.” In essence, if you’re adding constraints to a view in code, you need to specify it as follows:

tabBar.translatesAutoresizingMaskIntoConstraints = NO;

This is fairly annoying, but appears to be a tradeoff for supporting applications that partially use Auto Layout and partially do not.

Understanding unsatisfiability debug output

The interesting part of the debug output is the list of constraints. If it can, the output will describe them using Visual Format Language. Breaking it down, here is one of the above problem constraints:

"<NSLayoutConstraint:0x7464050 H:[UIView:0x7460e60]-(0)-| (Names: '|':UIView:0x745f950 )>"

In order, we see the following:

    1. The memory address of the layout constraint (and the fact that it is an NSLayoutConstraint)
    2. The Visual Format Language description of the constraint. Here, it’s a horizontal specifier. It specifies a view, no space, and then the right edge of the superview. This describes the view being pinned to the right edge of its superview.
    3. A legend of names in the constraint description. In this case, it lets us know the address of “|”, the superview reference.

Going through each constraint like this, focusing on the format strings, you can usually diagnose the issue. In the above example, we have three constraints: two that pin views to the right edge of their superview, and a third constraint that says the views are side by side with a spacer.

Intuitively, two views cannot both be pinned to the right edge and be spaced out side by side, so the constraints are impossible to satisfy, hence the exception. To solve the problem, we need to remove one of the pinning constraints, and our layout should be satisfiable.

Safari Books Online has the content you need

Below are some iOS books to help you develop apps for Apple’s popular platform, or you can check out all of the iOS books and training videos available from Safari Books Online. You can browse the content in preview mode or you can gain access to more information with a free trial or subscription to Safari Books Online.

Check out these iOS books available from Safari Books Online:

Using iOS 6 for Programmers. Second Edition, developers learn by creating 15 complete, fully tested iPhone/iPad/iPod Touch Apps, and in the process study 8,000+ lines of program code, including code that reveals the power of Apple’s newest iOS 6 SDK. By the time you have finished this book, you’ll have hands-on experience with virtually every type of iOS app, from games to media, productivity to web services, social networking to route tracking.
The iOS 6 Developer’s Cookbook: Core Recipes for Programmers, Fourth Edition by Erica Sadun brings all of the essential recipes you need to quickly start building successful iOS apps for iPhone, iPad, and iPod touch. Sadun has thoroughly revised this book to iOS 6, the latest version of Objective-C, and the latest Xcode development tools.
iOS 5 Programming Cookbook contains more than 100 new iOS 5 recipes covering iCloud, Automatic Reference Counting, storyboarding, graphics, animations, Grand Central Dispatch, threads, timers, audio and video and many other iOS 5 tools and techniques.
Assuming only a minimal working knowledge of Objective-C, and written in a friendly, easy-to-follow style, Beginning iOS 5 Development offers a complete soup-to-nuts course in iPhone, iPad, and iPod touch programming.

Start your FREE 10-day trial to Safari Books Online

About the Author

Allen Pike is a co-founder of Steamclock Software, where he and his team build wonderful apps for iOS and the web. Besides building software, he designs, teaches, and writes.
|

Comments are closed.

Facebook Twitter RSS feed