Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

In OSX 10.8 how do I constrain a subview to be the same size as its parent view

I have the default NSWindow created in a new application which has a single NSView. I then create a new NSViewController which has it's own XIB and a view. In the app delegate I do the obvious

self.mainViewController = [[MainViewController alloc] initWithNibName:@"MainViewController" bundle:nil];
[self.window.contentView addSubview:self.mainViewController.view];
self.mainViewController.view.frame = ((NSView*)self.window.contentView).bounds;

OK, how do I set a constraint in the new way to have my subview keep its size identical to the Window, i.e. it's superview. It doesn't seem to work automatically. Autoresizessubviews is ON for both views.

like image 790
ahwulf Avatar asked Nov 14 '12 03:11

ahwulf


1 Answers

Basically, you need to constrain four things:

  1. The leading space of your subview to its superview to be zero
  2. The top space of your subview to its superview to be zero
  3. The width of your subview to be equal to its superview's width
  4. The height of your subview to be equal to its superview's width

If the visual constraint isn't working out for you, you can build these four constraints individually in code. Use the method +constraintWithItem:attribute:relatedBy:toItem:attribute:multiplier: constant: to specify exact relationships between different views' attributes. For example, constraint #1 above might be expressed by:

[NSLayoutConstraint constraintWithItem:mySubview
                             attribute:NSLayoutAttributeLeading
                             relatedBy:NSLayoutRelationEqual
                                toItem:mySuperview
                             attribute:NSLayoutAttributeLeading
                            multiplier:1.0f
                              constant:0.0f]

and #3 might be:

[NSLayoutConstraint constraintWithItem:mySubview
                             attribute:NSLayoutAttributeWidth
                             relatedBy:NSLayoutRelationEqual
                                toItem:mySuperview
                             attribute:NSLayoutAttributeWidth
                            multiplier:1.0f
                              constant:0.0f]

Once you've built up those four constraints, you can add them to your superview as needed.

Note that there are multiple ways to achieve the same effect as above:

  • You might constrain the trailing space and bottom space instead of the width and height
  • You might constrain the center X and center Y instead of the leading and top spaces

You can also probably come up with the same constraints in a visual representation, as in Peter Hosey's answer. For example, an equal-width constraint might look like @"[mySubview(==mySuperview)]" with the appropriate views dictionary.

Keep in mind that the Auto Layout Guide is a wealth of information about constraints, including how to debug them when things go wrong.

like image 157
Tim Avatar answered Oct 02 '22 14:10

Tim