Skip to content

Instantly share code, notes, and snippets.

@mcmurrym
Created June 13, 2014 00:35
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 mcmurrym/46c993ee02d0bd70290e to your computer and use it in GitHub Desktop.
Save mcmurrym/46c993ee02d0bd70290e to your computer and use it in GitHub Desktop.
UIButton *button1 = [UIButton buttonWithType:UIButtonTypeCustom];
button1.translatesAutoresizingMaskIntoConstraints = NO;
button1.backgroundColor = [UIColor greenColor];
[button1 setTitle:@"button1" forState:UIControlStateNormal];
UIButton *button2 = [UIButton buttonWithType:UIButtonTypeCustom];
button2.translatesAutoresizingMaskIntoConstraints = NO;
button2.backgroundColor = [UIColor redColor];
[button2 setTitle:@"button2" forState:UIControlStateNormal];
UIButton *button3 = [UIButton buttonWithType:UIButtonTypeCustom];
button3.translatesAutoresizingMaskIntoConstraints = NO;
button3.backgroundColor = [UIColor yellowColor];
[button3 setTitle:@"button3" forState:UIControlStateNormal];
[self.view addSubview:button1];
[self.view addSubview:button2];
[self.view addSubview:button3];
NSDictionary *viewMap = NSDictionaryOfVariableBindings(button1, button2, button3);
NSDictionary *hmetrics = @{@"width" : @(106)};
NSArray *hConstraints =
[NSLayoutConstraint constraintsWithVisualFormat:@"H:|[button1(width)][button2(width)][button3(width)]|"
options:kNilOptions
metrics:hmetrics
views:viewMap];
[self.view addConstraints:hConstraints];
NSDictionary *vmetrics = @{@"height": @(100)};
NSArray *vConstraints1 =
[NSLayoutConstraint constraintsWithVisualFormat:@"V:|[button1(height)]"
options:kNilOptions
metrics:vmetrics
views:viewMap];
NSArray *vConstraints2 =
[NSLayoutConstraint constraintsWithVisualFormat:@"V:|[button2(height)]"
options:kNilOptions
metrics:vmetrics
views:viewMap];
NSArray *vConstraints3 =
[NSLayoutConstraint constraintsWithVisualFormat:@"V:|[button3(height)]"
options:kNilOptions
metrics:vmetrics
views:viewMap];
[self.view addConstraints:vConstraints1];
[self.view addConstraints:vConstraints2];
[self.view addConstraints:vConstraints3];
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment