Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6837417
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T23:28:29+00:00 2026-05-26T23:28:29+00:00

I have a single window with a single custom view in it, and I

  • 0

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:

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]];

Then the window doesn’t let me resize it.
If I add:

[customView setTranslatesAutoresizingMaskIntoConstraints:NO];

Then the custom view doesn’t appear (drawRect: seems to never be called).
I tried different ways (including the visual format @"|[customview]|") but I always run into the same problems. I know it could be done with the older autoresizing system, with:

[customView setAutoresizingMask:NSViewWidthSizable|NSViewHeightSizable];

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).

Does anyone know what is wrong and how I should use the Auto Layout system to get the result that I want?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T23:28:30+00:00Added an answer on May 26, 2026 at 11:28 pm

    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.

    Visual format constraints with regard to superview

    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]];
    

    Programmatic constraints for the edges

    (this should be equivalent to the visual format above)

    + (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]];
    }
    

    and

    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];
    

    Programmatic constraint for the size

    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]];
    

    The third approach is the one listed in the question and it may not work if there are further constraints. For example, without:

    [customView setTranslatesAutoresizingMaskIntoConstraints:NO];
    

    the original autoresize mask is applied as well, which leads to the behaviour described in the question: the window isn’t resized.

    As mentioned by Regexident, you can use:

    [_window visualizeConstraints:[contentView constraints]];
    

    to debug Auto Layout. It’s worth checking the console output as well.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have custom view in a HorizontalScrollView wrapped in a ScrollView and I want
In one of my first Cocoa applications I have just a single window with
I have a greasemonkey user script with this single line of code... window.close(); but
I load a single instance of a window on php-gtk, I have a button
I have a Windows Forms app, that has a single ElementHost containing a WPF
We have about 7 app servers running .NET windows services that ping a single
I have an single threaded, embedded application that allocates and deallocates lots and lots
I have a single user java program that I would like to have store
I have a single line CEikLabel in my application that needs to scroll text.
I have a custom NSView (it's one of many and they all live inside

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.