Skip to content

Instantly share code, notes, and snippets.

@justin
Created August 15, 2012 02:56
Show Gist options
  • Save justin/3355259 to your computer and use it in GitHub Desktop.
Save justin/3355259 to your computer and use it in GitHub Desktop.
NSView *contentView = [self.window contentView];
self.button = [[NSButton alloc] initWithFrame:NSMakeRect(0, 0, 200, 50.0f)];
[self.button setTranslatesAutoresizingMaskIntoConstraints:NO];
[self.button setTitle:@"This is a test button"];
[self.button setBordered:YES];
[contentView addSubview:self.button];
NSButton *button = self.button;
NSDictionary *views = NSDictionaryOfVariableBindings(contentView, button);
[contentView addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:[button(>=50)]" options:NSLayoutFormatAlignAllTop metrics:nil views:views]];
[contentView addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|-[button(>=50)]-|" options:NSLayoutFormatAlignAllBaseline metrics:nil views:views]];
[contentView addConstraints:@[
[NSLayoutConstraint constraintWithItem:self.button
attribute:NSLayoutAttributeCenterY
relatedBy:NSLayoutRelationEqual
toItem:contentView
attribute:NSLayoutAttributeCenterY
multiplier:1.0
constant:0]]];
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment