Recipe: Centering Views

To center views, associate their center properties (centerX and centerY) with the corresponding properties in their container. Recipe 5-3 introduces a pair of methods that retrieve a view’s superview and apply the equality relation between their centers.

Notice that these constraints are added to a parent view and not the child view. This is because constraints cannot reference views outside their own subtree. Here’s the error that’s generated if you attempt to do otherwise:

2012-06-24 16:09:14.736 HelloWorld[65437:c07] *** Terminating app due to uncaught  exception 'NSGenericException', reason: 'Unable to install constraint on view. Does the constraint reference something from outside the ...

Get The Core iOS Developer’s Cookbook, Fifth Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.