4

Hi I'm trying to constrain the max and min coordinate of an NSSplitView. I've created a view controller and assigned it as the delegate of an NSSplitView. The delegate methods get called however, the split view does not constrain to the position that I am trying to set it as. Any suggestions as to what I am doing wrong?

- (CGFloat)splitView:(NSSplitView *)splitView constrainMinCoordinate:(CGFloat)proposedMinimumPosition ofSubviewAt:(NSInteger)dividerIndex 
{
    NSLog(@"Constrain min");

    if (proposedMinimumPosition < 75) 
    {
        proposedMinimumPosition = 75;
    }

    return proposedMinimumPosition;
}

- (CGFloat)splitView:(NSSplitView *)splitView constrainMaxCoordinate:(CGFloat)proposedMax ofSubviewAt:(NSInteger)dividerIndex
{
    NSLog(@"Constrain max");

    if (proposedMax > 200) 
    {
        proposedMax = 200;
    }

    return proposedMax ;
}
David
  • 14,205
  • 20
  • 97
  • 144

2 Answers2

1

Let's assume you want each one of two sections on a vertical splitter to be at least 70.0 high, then what you'd do this:

- (CGFloat)     splitView:(NSSplitView *)splitView
   constrainMinCoordinate:(CGFloat)proposedMin
              ofSubviewAt:(NSInteger)dividerIndex
{
    return 70.0;
}

- (CGFloat)     splitView:(NSSplitView *)splitView
   constrainMaxCoordinate:(CGFloat)proposedMin
              ofSubviewAt:(NSInteger)dividerIndex
{
    return splitView.frame.size.height - 70.0;
}

The reason for the subtraction is to dynamically account for any resizing (with autolayout, for example) of the overall NSplitView instance. If you're working with a horizontal one, then you'd need to calculate against .width instead of .height. If you have more than 2 subviews, the idea can be extended by looking at dividerIndex and applying values as you see fit.

Fitter Man
  • 682
  • 8
  • 17
0

I solved the problem by doing this.

- (BOOL)splitView:(NSSplitView *)splitView canCollapseSubview:(NSView *)subview 
{
    return NO;
}
David
  • 14,205
  • 20
  • 97
  • 144