Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>With Auto Layout, there are (at least) three possible ways to constrain a view so that it occupies the entire window’s content view, resizing when appropriate.</p> <h1>Visual format constraints with regard to superview</h1> <pre><code>NSView *contentView = [_window contentView]; MyView *customView = [[MyView alloc] initWithFrame:[contentView bounds]]; [customView setTranslatesAutoresizingMaskIntoConstraints:NO]; [contentView addSubview:customView]; NSDictionary *views = NSDictionaryOfVariableBindings(customView); [contentView addConstraints: [NSLayoutConstraint constraintsWithVisualFormat:@"H:|[customView]|" options:0 metrics:nil views:views]]; [contentView addConstraints: [NSLayoutConstraint constraintsWithVisualFormat:@"V:|[customView]|" options:0 metrics:nil views:views]]; </code></pre> <h1>Programmatic constraints for the edges</h1> <p>(this should be equivalent to the visual format above)</p> <pre><code>+ (void)addEdgeConstraint:(NSLayoutAttribute)edge superview:(NSView *)superview subview:(NSView *)subview { [superview addConstraint:[NSLayoutConstraint constraintWithItem:subview attribute:edge relatedBy:NSLayoutRelationEqual toItem:superview attribute:edge multiplier:1 constant:0]]; } </code></pre> <p>and</p> <pre><code>NSView *contentView = [_window contentView]; MyView *customView = [[MyView alloc] initWithFrame:[contentView bounds]]; [customView setTranslatesAutoresizingMaskIntoConstraints:NO]; [contentView addSubview:customView]; [[self class] addEdgeConstraint:NSLayoutAttributeLeft superview:contentView subview:customView]; [[self class] addEdgeConstraint:NSLayoutAttributeRight superview:contentView subview:customView]; [[self class] addEdgeConstraint:NSLayoutAttributeTop superview:contentView subview:customView]; [[self class] addEdgeConstraint:NSLayoutAttributeBottom superview:contentView subview:customView]; </code></pre> <h1>Programmatic constraint for the size</h1> <pre><code>NSView *contentView = [_window contentView]; MyView *customView = [[MyView alloc] initWithFrame:[contentView bounds]]; [customView setTranslatesAutoresizingMaskIntoConstraints:NO]; [contentView addSubview:customView]; [contentView addConstraint: [NSLayoutConstraint constraintWithItem:customView attribute:NSLayoutAttributeWidth relatedBy:NSLayoutRelationEqual toItem:contentView attribute:NSLayoutAttributeWidth multiplier:1 constant:0]]; [contentView addConstraint: [NSLayoutConstraint constraintWithItem:customView attribute:NSLayoutAttributeHeight relatedBy:NSLayoutRelationEqual toItem:contentView attribute:NSLayoutAttributeHeight multiplier:1 constant:0]]; </code></pre> <hr> <p>The third approach is the one listed in the question and it may not work if there are further constraints. For example, without:</p> <pre><code>[customView setTranslatesAutoresizingMaskIntoConstraints:NO]; </code></pre> <p>the original autoresize mask is applied as well, which leads to the behaviour described in the question: the window isn’t resized.</p> <p>As mentioned by Regexident, you can use:</p> <pre><code>[_window visualizeConstraints:[contentView constraints]]; </code></pre> <p>to debug Auto Layout. It’s worth checking the console output as well.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload