in Education by
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? JavaScript questions and answers, JavaScript questions pdf, JavaScript question bank, JavaScript questions and answers pdf, mcq on JavaScript pdf, JavaScript questions and solutions, JavaScript mcq Test , Interview JavaScript questions, JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)

1 Answer

0 votes
by
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.

Related questions

0 votes
    Using the Apple OS X Cocoa framework, how can I post a sheet (slide-down modal dialog) on the ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Mar 24, 2022 in Education by JackTerrance
0 votes
    I have a problem with table-layout:fixed and width of inner columns if their content wider than column. ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Aug 1, 2022 in Education by JackTerrance
0 votes
    I'm using google street view , i want to make users look around the street when they enter my ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Feb 9, 2022 in Education by JackTerrance
0 votes
    I'm using google street view , i want to make users look around the street when they enter my ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Feb 5, 2022 in Education by JackTerrance
0 votes
    How we can layout subviews in a view in Objective-C?...
asked Nov 10, 2020 in Technology by JackTerrance
0 votes
    I'm using Angular Material in my application. I have input fields like this: E-mail Which produces a ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked May 7, 2022 in Education by JackTerrance
0 votes
    I'm using Angular Material in my application. I have input fields like this: E-mail Which produces a ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked May 7, 2022 in Education by JackTerrance
0 votes
    I have a relative layout that has an ImageView and and a textview aligned to right of the ImageView. ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Feb 18, 2022 in Education by JackTerrance
0 votes
    I have a relative layout that has an ImageView and and a textview aligned to right of the ImageView. ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Feb 18, 2022 in Education by JackTerrance
0 votes
    I have a relative layout that has an ImageView and and a textview aligned to right of the ImageView. ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Feb 16, 2022 in Education by JackTerrance
0 votes
    1 What is the default view of writer?a Print Viewb. Web Layout Viewc.full screen view 2. Where on the screen ... c. none of these Select the correct answer from above options...
asked Dec 25, 2021 in Education by JackTerrance
0 votes
    I have an image of a puzzle piece that i need to resize in order for both the pieces I need to ... , JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Apr 19, 2022 in Education by JackTerrance
0 votes
    Custom Role in Oracle Fusion : How to Create PO and Receiving Inquiry(View Only) Role in Oracle Fusion?...
asked Nov 22, 2020 in Technology by JackTerrance
0 votes
    If I have a view in SQL which contains various computed columns, some of which could be very expensive, ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Feb 19, 2022 in Education by JackTerrance
0 votes
    $('#some-table').DataTable({ "language": { "url": "{{ asset('/js/plugins/lang.json') }} ... , JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Jul 26, 2022 in Education by JackTerrance
...