Skip to content

Instantly share code, notes, and snippets.

@lukeredpath
Created January 31, 2013 16:44
Show Gist options
  • Save lukeredpath/4684242 to your computer and use it in GitHub Desktop.
Save lukeredpath/4684242 to your computer and use it in GitHub Desktop.
An alternative higher level API over NSLayoutConstraint, NOT inspired by ASCII art.
NSLayoutConstraintBuilder *builder = [NSLayoutConstraintBuilder builder];
UIView *mySubview = [[UIView alloc] initWithFrame:CGRectZero];
// API deliberately mirrors the terminology of IB:
[builder pin:mySubview toWidth:200];
[builder pin:mySubview toHeight:200];
[builder pin:mySubview toSize:CGSizeMake(200, 200)];
// Relationship with parent view:
// this could be offset, constant or padding, not sure which is best
[builder pin:mySubview leadingSpaceToSuperview:self.parentView offset:10];
[builder pin:mySubview trailingSpaceToSuperview:self.parentView offset:10];
// etc.., or perhaps:
// will this always be the parent view? If so, no need to pass it in, we can just ask for the superview
[builder pin:mySubview toParentView:NSLayoutAttributeTop offset:10];
[builder pin:mySubview toParentView:NSLayoutAttributeBottom offset:0];
// a few higher level ideas:
// equivalent to pinning top, bottom, leading and trailing, no dimension constraints
[builder stretchWithSuperView:mySubview];
// assuming mySubview has explicit dimensions, pins it's centerY and centerX to superview
[builder centerViewInParentView:mySubview];
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment