Skip to content

Instantly share code, notes, and snippets.

@wilg
Created July 19, 2014 19:39
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 wilg/b9cdd0274f24c743ad52 to your computer and use it in GitHub Desktop.
Save wilg/b9cdd0274f24c743ad52 to your computer and use it in GitHub Desktop.
Embed an NSViewController in another view with auto layout
- (void)embedViewController:(NSViewController *)controller inView:(NSView *)container {
[container addSubview:controller.view];
controller.view.translatesAutoresizingMaskIntoConstraints = NO;
controller.view.frame = container.bounds;
[container addConstraint:[NSLayoutConstraint constraintWithItem:container
attribute:NSLayoutAttributeTop
relatedBy:NSLayoutRelationEqual
toItem:controller.view
attribute:NSLayoutAttributeTop
multiplier:1.0
constant:0.0]];
[container addConstraint:[NSLayoutConstraint constraintWithItem:container
attribute:NSLayoutAttributeLeading
relatedBy:NSLayoutRelationEqual
toItem:controller.view
attribute:NSLayoutAttributeLeading
multiplier:1.0
constant:0.0]];
[container addConstraint:[NSLayoutConstraint constraintWithItem:container
attribute:NSLayoutAttributeBottom
relatedBy:NSLayoutRelationEqual
toItem:controller.view
attribute:NSLayoutAttributeBottom
multiplier:1.0
constant:0.0]];
[container addConstraint:[NSLayoutConstraint constraintWithItem:container
attribute:NSLayoutAttributeTrailing
relatedBy:NSLayoutRelationEqual
toItem:controller.view
attribute:NSLayoutAttributeTrailing
multiplier:1.0
constant:0.0]];
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment