3

I have an interface designed as below set up with auto layout in the XIB.

[Left Pane]-[Center Pane]-[Right Pane]
[            Bottom Pane             ]

Currently it supports max and min widths/heights for each of the panes as well as collapsing subviews by either double clicking on the divider or with a NSSegmentedControl. What I would like to do now is to animate the collapse that occurs when a user toggles the NSSegmentedControl. I have seen many examples of setting up animation with NSSplitView for collapsing a sub-view but none that accomplish it using Auto Layout.

I have tried to follow the demo for animating constraints provided in the WWDC 2012 video on Auto Layout Demo's. But as this is my first time enabling Auto Layout I have not yet figured out how to do it.

Below is what I currently have

NSArray *constraints = [self.leftPane constraints];
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"firstAttribute = %d", NSLayoutAttributeWidth];
NSArray *filteredArray = [constraints filteredArrayUsingPredicate:predicate];

[self.leftPane removeConstraints:filteredArray];

[self.lcrSplitView layoutSubtreeIfNeeded];

[NSAnimationContext runAnimationGroup:^(NSAnimationContext *context) {
    [context setAllowsImplicitAnimation:YES];

    NSLayoutConstraint *newWidth = [NSLayoutConstraint constraintWithItem:self.leftPane
                                                                attribute:NSLayoutAttributeWidth
                                                                relatedBy:NSLayoutRelationEqual
                                                                   toItem:nil
                                                                attribute:NSLayoutAttributeNotAnAttribute
                                                               multiplier:0.0f
                                                                 constant:0.0f];

    [self.leftPane addConstraint:newWidth];


    [self.lcrSplitView layoutSubtreeIfNeeded];
} completionHandler:^{}];
xizor
  • 1,554
  • 2
  • 16
  • 35
  • Did you ever figure this out? I have the exact same story as you, down to the fact that I just followed that WWDC 2012 demo. The only thing that differs between us is that I actually have a Top pane in addition to the left, center, right, and bottom panes. – ArtOfWarfare Nov 17 '13 at 04:54
  • I don't have a solution that animates yet, but this may be of interest to others: http://stackoverflow.com/a/20027805/901641 – ArtOfWarfare Nov 17 '13 at 06:23
  • @ArtOfWarfare, I ended up getting something working but it was very convoluted and was prone to errors. For some reason it would work fine on Mountain Lion but not on Mavericks (where it would be extremely jerky). So I ended up scrapping it and creating my own NSViewController subclass that contained 4 sub-views and rolling my own customized NSSplitView. I also moved away from auto layout for this project and just used spring and struts largely due to this issue causing so much of a headache. I know it hurts to hear but I recommend just creating your own split view implementation. ... – xizor Nov 18 '13 at 23:32
  • ... It took me about 5 hours to get something really robust whereas I had already spent 40-80 debugging and finking with the above issues. – xizor Nov 18 '13 at 23:33
  • It's good to hear it only takes 5 hours. I made a split view class for the original iOS SDK years ago, so it wouldn't be anything new for me. I just want to use as many standard tools as possible. For now, I'll stick with using the private method that I discovered NSSplitView has (I described accessing it in the answer I linked to) and petition Apple to add it to the public SDK. – ArtOfWarfare Nov 19 '13 at 15:04

1 Answers1

1

For anyone targeting 10.11 El Capitan, this did the trick for me.

splitViewItem.collapsed = YES;
Daniel Nordh
  • 600
  • 6
  • 9
  • This is perfect, thanks! Just to add a bit more, the `collapsed` property of `NSSplitViewItem` was introduced in 10.10. Also, to animate the transition use `splitViewItem.animator.collapsed = YES;` – Demitri Jan 09 '19 at 21:06