Skip to content

Instantly share code, notes, and snippets.

@yabenatti
Last active June 15, 2017 13:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save yabenatti/6fddb365203482b3b209267ccd87b5d9 to your computer and use it in GitHub Desktop.
Save yabenatti/6fddb365203482b3b209267ccd87b5d9 to your computer and use it in GitHub Desktop.
Programatic Constraints
- (void)setProgramaticConstraints {
self.myView = [UIView new];
self.myView.translatesAutoresizingMaskIntoConstraints = NO;
[self.myView setBackgroundColor:COLOR_WHITE_SMOKE];
[self.view addSubview:self.myView];
[self.myView addConstraint:[NSLayoutConstraint constraintWithItem:self.myView
attribute:NSLayoutAttributeHeight
relatedBy:NSLayoutRelationEqual
toItem:nil
attribute:NSLayoutAttributeNotAnAttribute
multiplier:1.0
constant:1.0f]];
NSLayoutConstraint *myViewTopConstraint = [NSLayoutConstraint
constraintWithItem:self.myView attribute:NSLayoutAttributeTop
relatedBy:NSLayoutRelationEqual toItem:self.view
attribute:NSLayoutAttributeBottom multiplier:1.0 constant:0.0f];
NSLayoutConstraint *myViewBottomConstraint = [NSLayoutConstraint
constraintWithItem:self.myView attribute:NSLayoutAttributeBottom
relatedBy:NSLayoutRelationEqual toItem:self.view
attribute:NSLayoutAttributeBottom multiplier:1.0 constant:0.0f];
NSLayoutConstraint *myViewLeadingConstraint = [NSLayoutConstraint
constraintWithItem:self.myView attribute:NSLayoutAttributeLeading
relatedBy:NSLayoutRelationEqual toItem:self.view
attribute:NSLayoutAttributeLeading multiplier:1.0 constant:0.0f];
NSLayoutConstraint *myViewTrailingConstraint = [NSLayoutConstraint
constraintWithItem:self.myView attribute:NSLayoutAttributeTrailing
relatedBy:NSLayoutRelationEqual toItem:self.view
attribute:NSLayoutAttributeTrailing multiplier:1.0 constant:0.0f];
[self.view addConstraints:@[myViewTopConstraint, myViewBottomConstraint, myViewLeadingConstraint, myViewTrailingConstraint]];
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment