Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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
You can’t perform that action at this time.