When debugging autolayout what is the meaning of the autoresizing mask strings such as h=--& v=-&-?
CocoaAutolayoutCocoa Problem Overview
I guess they must refer to the struts and springs model but I can't find any mention of them. When you NSLog
constraint they sometimes appear as the description string of the undocumented class NSAutoresizingMaskLayoutConstraint
. I have noticed at least 3 different types: h=---
, h=--&
, h=-&-
with horizontal and vertical version.
They turn up a lot when debugging over constrained layouts.
Cocoa Solutions
Solution 1 - Cocoa
If you specify autoresizing masks instead of constraints, or specify no constraints at all, then the view will have NSAutoResizingMaskLayoutConstraint constraints as opposed to NSLayoutConstraints. If you set translatesAutoresizingMaskIntoConstraints
to NO
, then these constraints do not appear. You can't mix and match on a single view, or you get unsatisfiable constraint errors.
I set up a quick test project with various combinations of autoresizing masks and the logging format is pretty straightforward.
h=
orv=
indicates that we are talking about contraints in the horizontal or vertical direction.-
indicates a fixed size&
indicates a flexible size- The order of symbols represents margin, dimension, margin
Therefore, h=&-&
means you have flexible left and right margins and a fixed width, v=-&-
means fixed top and bottom margins and flexible height, and so forth.
Solution 2 - Cocoa
If you watch the WWDC 2012 video on Best Practices for Mastering Auto Layout, there is a section in there where the presenter mentions that this is the syntax for views that use Autoresizing Masks and NOT constraints. There's no visual format associated with these like there is for NSLayoutConstraint.
Solution 3 - Cocoa
Adding to jrturton's answer, the best information I've found to understand the constraint descriptions is the Visual Format Language documentation that you need to create constraints in code. The language is documented as a formal grammar so it may take a minute to absorb it all.
Give you an example:
<NSLayoutConstraint:0x10ada8a70 H:|-(44)-[UIButton:0x10ac5dc30] (Names: '|':UIView:0x10ac60470)>
This is a Horizontal Orientation constraint (H:) The linkage is 44 pixels from a superview (|) to a UIButton The named superview is a UIView (Names: '|':UIView:) — important to know because you don’t need to be constrained by the immediate superview
The hex values are all valid addresses of your UI elements. When you breakpoint on all exceptions and pause on the constraint conflict, you can use po address to see the object of the constraint. In my case:
(lldb) po 0x10ac5dc30
<UIButton: 0x10ac5dc30; frame = (44 199; 30 30); opaque = NO; autoresize = RM+BM; layer = <CALayer: 0x10ac5ddf0>>
(lldb) po 0x10ac60470
<UIView: 0x10ac60470; frame = (0 64; 320 504); autoresize = RM+BM; animations = { position=<CABasicAnimation: 0x10ac5ec70>; bounds=<CABasicAnimation: 0x10ac62250>; }; layer = <CALayer: 0x10ac60530>>