Visual Format Constraints

The preceding section shows you how to create single constraint relations. A second NSLayoutConstraint class method builds constraints using a text-based visual format language. Think of it as ASCII art for Objective-C nerds. Here’s a simple example:

[self.view addConstraints: [NSLayoutConstraint     constraintsWithVisualFormat:@"V:[leftLabel]-15-[rightLabel]"     options:0     metrics:nil     views:NSDictionaryOfVariableBindings(leftLabel, rightLabel)]];

This request creates all the constraints that satisfy the relation or relations specified in the visual format string. These strings, which you will see in many examples in following sections, describe how views relate to each other along ...

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.