Skip to content

Instantly share code, notes, and snippets.

@spr
Created January 30, 2013 19:02
Show Gist options
  • Save spr/4675763 to your computer and use it in GitHub Desktop.
Save spr/4675763 to your computer and use it in GitHub Desktop.
Example code for http://stackoverflow.com/posts/14531884 answer
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
button.translatesAutoresizingMaskIntoConstraints = NO;
[button setTitle:@"Press ME!" forState:UIControlStateNormal];
UILabel *label = [[UILabel alloc] init];
label.translatesAutoresizingMaskIntoConstraints = NO;
label.text = @"Hi Mom!";
[self.view addSubview:button];
[self.view addSubview:label];
[self.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:[button]-[label]" options:0 metrics:nil views:NSDictionaryOfVariableBindings(button, label)]];
[self.view addConstraint:[NSLayoutConstraint constraintWithItem:button attribute:NSLayoutAttributeBottom relatedBy:NSLayoutRelationEqual toItem:self.view attribute:NSLayoutAttributeCenterY multiplier:1.0 constant:4.0]];
[self.view addConstraint:[NSLayoutConstraint constraintWithItem:button attribute:NSLayoutAttributeCenterX relatedBy:NSLayoutRelationEqual toItem:self.view attribute:NSLayoutAttributeCenterX multiplier:1.0 constant:0.0]];
[self.view addConstraint:[NSLayoutConstraint constraintWithItem:label attribute:NSLayoutAttributeCenterX relatedBy:NSLayoutRelationEqual toItem:self.view attribute:NSLayoutAttributeCenterX multiplier:1.0 constant:0.0]];
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment