Skip to content

Instantly share code, notes, and snippets.

@kgn
Created October 28, 2013 19:04
Show Gist options
  • Save kgn/7202655 to your computer and use it in GitHub Desktop.
Save kgn/7202655 to your computer and use it in GitHub Desktop.
Handy Auto Layout
CGFloat distance = 10;
// Center two views side-by-side
[parentItem addConstraint:[NSLayoutConstraint constraintWithItem:leftItem attribute:NSLayoutAttributeRight relatedBy:NSLayoutRelationLessThanOrEqual toItem:parentItem attribute:NSLayoutAttributeCenterX multiplier:1 constant:-distance]];
[parentItem addConstraint:[NSLayoutConstraint constraintWithItem:rightItem attribute:NSLayoutAttributeLeft relatedBy:NSLayoutRelationLessThanOrEqual toItem:parentItem attribute:NSLayoutAttributeCenterX multiplier:1 constant:distance]];
// Center two views, one ontop of the other
[parentItem addConstraint:[NSLayoutConstraint constraintWithItem:topItem attribute:NSLayoutAttributeBottom relatedBy:NSLayoutRelationLessThanOrEqual toItem:parentItem attribute:NSLayoutAttributeCenterY multiplier:1 constant:-distance]];
[parentItem addConstraint:[NSLayoutConstraint constraintWithItem:bottomItem attribute:NSLayoutAttributeTop relatedBy:NSLayoutRelationLessThanOrEqual toItem:parentItem attribute:NSLayoutAttributeCenterY multiplier:1 constant:distance]];
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment