Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to make a custom view resize with the window with Cocoa Auto Layout?
    primarykey
    data
    text
    <p>I have a single window with a single custom view in it, and I want the custom view to resize with the window so that it entirely fills it at any time. If I write:</p> <pre><code>NSView *contentView = [self.window contentView]; CustomView *customView = [[CustomView alloc] initWithFrame:[contentView bounds]]; [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> <p>Then the window doesn't let me resize it.<br> If I add:</p> <pre><code>[customView setTranslatesAutoresizingMaskIntoConstraints:NO]; </code></pre> <p>Then the custom view doesn't appear (<code>drawRect:</code> seems to never be called). I tried different ways (including the visual format <code>@"|[customview]|"</code>) but I always run into the same problems. I know it could be done with the older autoresizing system, with:</p> <pre><code>[customView setAutoresizingMask:NSViewWidthSizable|NSViewHeightSizable]; </code></pre> <p>but I want to use the Cocoa Auto Layout system, and I want to use it for more complicated cases (like several custom views that always fill the window).</p> <p>Does anyone know what is wrong and how I should use the Auto Layout system to get the result that I want?</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

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