Quick tip on working with iOS Auto Layout by code

icon512.png 512×512 pixels

A quick tip for those of you working with iOS Auto-Layout in code. During your development, you can test whether a particular view is sufficiently constrained/specified, by asking hasAmbiguousLayout By using in in NSLog, you can query the view’s constraint satisfaction as follows:

NSLog(@"View <%@:0x%0x> has ambiguous layout? %@", self.aView.description, self.aView.hasAmbiguousLayout);

NSView Reference

hasAmbiguousLayout Returns whether the constraints impacting the layout of the view incompletely specify the location of the view.

  • (BOOL)hasAmbiguousLayout Return Value YES if the view’s location is incompletely specified, NO otherwise.

Discussion This method checks to see if there is any other frame the view could have that would also satisfy the constraints on the view. This is an expensive operation and is not run as part of the normal layout process, but can be useful when debugging whether a given interface has been specified with a sufficient number of constraints to ensure consistent layout. This method is automatically invoked when a window has been told to visualize constraints with the visualizeConstraints: method.

This method should only be used for debugging constraint-based layout. No application should ship with calls to this method as part of its operation.

Availability Available in OS X v10.7 and later. See Also – exerciseAmbiguityInLayout Declared In NSLayoutConstraint.h