Ronald S. Bultje
Ronald S. Bultje

Reputation: 11174

Autolayout-based cocoa split view snaps back to old size after each resize

To demonstrate my issue, I've created a simple test application with interface builder, which is the default window with the default viewcontroller attached to it (which is linked to the ViewController.m class in XCode), and the viewcontroller (in interface builder) contains a vertical split view. The right subview of this split view is then linked to an "IBOutlet NSView *right;" in the ViewController.h:ViewController interface.

Then lastly, in the ViewController.m, I have the following code:

+ (void)addConstraint:(NSLayoutAttribute)type relatedBy:(NSLayoutRelation)relation
            superview:(NSView *)superview subview:(NSView *)subview
{
    [superview addConstraint:[NSLayoutConstraint constraintWithItem:subview
                                                          attribute:type
                                                          relatedBy:relation
                                                             toItem:superview
                                                          attribute:type
                                                         multiplier:1
                                                           constant:0]];
}

- (void)viewDidLoad {
    [super viewDidLoad];

    // Do any additional setup after loading the view.
    int numObjects = 3;
    NSMutableArray *array = [[NSMutableArray alloc] init];
    for (int i = 0; i < numObjects; i++) {
        NSTextField *label = [[NSTextField alloc] init];
        [label setEditable:NO];
        label.bezeled = NO;
        label.editable = NO;
        label.drawsBackground = NO;
        [label setTranslatesAutoresizingMaskIntoConstraints:NO];
        [label setAutoresizesSubviews:YES];
        [label setStringValue:[NSString stringWithFormat:@"%d", i]];
        [array addObject:label];
    }

    NSStackView *stackView = [NSStackView stackViewWithViews:array];
    [stackView setTranslatesAutoresizingMaskIntoConstraints:NO];
    [stackView setAutoresizesSubviews:YES];
    stackView.orientation = NSUserInterfaceLayoutOrientationVertical;
    [right addSubview:stackView];
    [[self class] addConstraint:NSLayoutAttributeLeft   relatedBy:NSLayoutRelationEqual superview:right subview:stackView];
    [[self class] addConstraint:NSLayoutAttributeRight  relatedBy:NSLayoutRelationEqual superview:right subview:stackView];
    for (int i = 0; i < [array count]; i++) {
        NSView *view = [array objectAtIndex:i];
        [[self class] addConstraint:NSLayoutAttributeLeft   relatedBy:NSLayoutRelationEqual superview:stackView subview:view];
        [[self class] addConstraint:NSLayoutAttributeRight  relatedBy:NSLayoutRelationEqual superview:stackView subview:view];
    }
}

Resizing using the split view's handle bar works, hurray! Now, the problem is, as soon as I change the "int numObjects = 3;" to any number greater than 3 (for example: "int numObjects = 4;"), the resizing behaviour breaks. Now, if I drag the split handle between the left/right halves, it resizes appropriately while my mouse button is still down; however, if I let go of the mouse button, the split view snaps back to its old (pre-resize) configuration, with the right half being the smallest possible size.

What am I doing wrong?

Upvotes: 2

Views: 654

Answers (1)

Ronald S. Bultje
Ronald S. Bultje

Reputation: 11174

I eventually figured this out by google-fu: try to find the right keywords to find the correct stackoverflow solution. This post explains how to solve it, basically by changing the "holding priority" of the right pane in the split view from 250 to 251.

Upvotes: 2

Related Questions