简体   繁体   中英

How do I stop an NSView subview being resized with Auto Layout

I am having a problem with the new Auto Layout functionality. I add an NSView called tableView as a subview of workingBox. workingBox also contains some other subviews that were added with Interface Builder

[self.window setFrame:frame display:YES animate:YES];
[workingBox addSubview:tableView];

My problem is that if any of the other subviews touch tableView then they will get resized in the animation. The subviews that are not near where tableView is being added remain the correct size. I have already tried setting NSViewNotSizable resizeMask on the subviews that are getting resized but it does not work. The only thing that works is moving the subview upwards (into the wrong position.


EDIT: I have added a constraint in code:

NSDictionary *viewsDictionary = NSDictionaryOfVariableBindings(divider);
NSArray *constraints = [NSLayoutConstraint constraintsWithVisualFormat:@"V:[divider(==1)]"
                                                               options:0
                                                               metrics:nil
                                                                 views:viewsDictionary];
[workingBox addConstraints:constraints];  

This keeps the NSView at 1px height but if I lock it to the top it prevents the superview growing

I could not make this work by adding a constraint in code. However in Interface Builder I could see that there was a constraint for the NSView in question that was named 'Bottom Alignment'. I changed this to a priority of 1 in the inspector and it stopped the NSView getting stretched down when the window expanded. I think this auto added constraint that I mentioned in my comment above had been the problem all along.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM