24

A simple NSLayoutConstraint expression parser for more readable autolayout code. By Marco Arment and released under the MIT license.

Apple’s visual format syntax is helpful, but it cannot express all types of autolayout constraints, and it’s not ideal for simple values. Creating NSLayoutConstraints manually is more powerful and covers all possibilities, but is extremely verbose and hard to read.

CompactConstraint brings compact, readable syntax to creating NSLayoutConstraints manually. In short, rather than writing Cocoa autolayout constraints like this:

[self.view addConstraint:[NSLayoutConstraint constraintWithItem:emailLabel
                                                      attribute:NSLayoutAttributeLeft
                                                      relatedBy:NSLayoutRelationGreaterThanOrEqual
                                                         toItem:emailField
                                                      attribute:NSLayoutAttributeLeft
                                                     multiplier:1.0f
                                                       constant:0.0f]];
[self.view addConstraint:[NSLayoutConstraint constraintWithItem:spinner
                                                      attribute:NSLayoutAttributeLeft
                                                      relatedBy:NSLayoutRelationEqual
                                                         toItem:logInButton
                                                      attribute:NSLayoutAttributeRight
                                                     multiplier:1.0f
                                                       constant:10.0f]];
[self.view addConstraint:[NSLayoutConstraint constraintWithItem:preview
                                                      attribute:NSLayoutAttributeHeight
                                                      relatedBy:NSLayoutRelationEqual
                                                         toItem:preview
                                                      attribute:NSLayoutAttributeWidth
                                                     multiplier:0.625f
                                                       constant:0.0f]];

CompactConstraint lets you write them like this:

[self.view addCompactConstraint:@"emailLabel.left >= emailField.left"
                        metrics:metrics
                          views:views];
[self.view addCompactConstraint:@"spinner.left = logInButton.right + 10"
                        metrics:metrics
                          views:views];
[self.view addCompactConstraint:@"preview.height = preview.width / 1.6"
                        metrics:metrics
                          views:views];

Or this:

[self.view addCompactConstraints:@[@"emailLabel.left >= emailField.left",
                                   @"spinner.left = logInButton.right + 10",
                                   @"preview.height = preview.width / 1.6"]
                         metrics:metrics
                           views:views];

CompactConstraint on GitHub