Skip to content

Instantly share code, notes, and snippets.

@tayhalla
Created July 11, 2014 17:56
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 tayhalla/70b8e2f1fae112eb44e3 to your computer and use it in GitHub Desktop.
Save tayhalla/70b8e2f1fae112eb44e3 to your computer and use it in GitHub Desktop.
/*
Creates a green btn and sets its position equal to a rect of {50.0f, 50.0f, 200.0f, 300.0f}
*/
UIButton *btn = [[UIButton alloc] init];
btn.backgroundColor = [UIColor greenColor];
[btn setTranslatesAutoresizingMaskIntoConstraints:NO];
[self.view addSubview:btn];
NSLayoutConstraint *widthConstraint = [NSLayoutConstraint constraintWithItem:btn attribute:NSLayoutAttributeWidth relatedBy:NSLayoutRelationEqual toItem:nil attribute:0 multiplier:0.0 constant:200];
NSLayoutConstraint *heightConstraint = [NSLayoutConstraint constraintWithItem:btn attribute:NSLayoutAttributeHeight relatedBy:NSLayoutRelationEqual toItem:nil attribute:0 multiplier:0.0 constant:300];
NSLayoutConstraint *vertConstraint = [NSLayoutConstraint constraintWithItem:btn attribute:NSLayoutAttributeTop relatedBy:NSLayoutRelationEqual toItem:self.view attribute:NSLayoutAttributeTop multiplier:0.0 constant:50];
NSLayoutConstraint *horizConstraint = [NSLayoutConstraint constraintWithItem:btn attribute:NSLayoutAttributeLeft relatedBy:NSLayoutRelationEqual toItem:self.view attribute:NSLayoutAttributeLeft multiplier:0.0 constant:50];
[self.view addConstraints:@[widthConstraint, heightConstraint, vertConstraint, horizConstraint]];
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment