2

I'm having a bit of difficulty getting a NSSplitView to behave itself.

What I have at the moment is:

NSWindow
   NSView
      NSSplitView
         navView <NSView>
         contentView <NSView>

The problem I'm having is with the splitter shifting position when I resize the window.

In the split view delegate I've already got:

-(CGFloat)splitView:(NSSplitView *)splitView constrainMaxCoordinate:(CGFloat)proposedMaximumPosition ofSubviewAt:(NSInteger)dividerIndex{
    return 200;
}

-(CGFloat)splitView:(NSSplitView *)splitView constrainMinCoordinate:(CGFloat)proposedMinimumPosition ofSubviewAt:(NSInteger)dividerIndex{
    return 200;

}

Despite this the splitter still moved when I resize the window.

if I click ont he splitter, it snaps back to 200px as it should. How can I stop this from moving?

I've tried Autolayout, which is a bit of a nightmare to use, so I've literally disabled it and manually tried to do it with no joy..

Any ideas?

1
  • 1
    just to add, this goes totally awry when I programatically set the window frame to accommodate a larger subview in contentView! Jul 3, 2013 at 7:16

3 Answers 3

3

I wrote the above code for Swift and extended it with the possibility to define, whether the left or the right view has to be preferred:

var preferringLeftSideOfSplitView = true

func splitView(splitView: NSSplitView, resizeSubviewsWithOldSize oldSize: NSSize) {
    var dividerThickness = splitView.dividerThickness

    var leftRect = splitView.subviews[0].frame
    var rightRect = splitView.subviews[1].frame

    // Resizing and placing the left view
    splitView.subviews[0].setFrameOrigin(NSMakePoint(0, 0))
    if self.preferringLeftSideOfSplitView == true {
        splitView.subviews[0].setFrameSize(NSMakeSize(leftRect.width, splitView.frame.size.height))
    } else {
        splitView.subviews[0].setFrameSize(NSMakeSize(splitView.frame.size.width - rightRect.width - dividerThickness, splitView.frame.size.height))
    }

    // Resizing and placing the right view
    if self.preferringLeftSideOfSplitView == true {
        splitView.subviews[1].setFrameOrigin(NSMakePoint(leftRect.size.width + dividerThickness, 0))
        splitView.subviews[1].setFrameSize(NSMakeSize(splitView.frame.size.width - leftRect.size.width - dividerThickness, splitView.frame.size.height))
    } else {
        splitView.subviews[1].setFrameOrigin(NSMakePoint(splitView.frame.size.width - rightRect.width, 0))
        splitView.subviews[1].setFrameSize(NSMakeSize(rightRect.size.width, splitView.frame.size.height))
    }
}
2

I've worked it out...

-(void)splitView:(NSSplitView *)sender resizeSubviewsWithOldSize:(NSSize)oldSize
{
    CGFloat dividerThickness = [sender dividerThickness];
    NSRect leftRect  = [[[sender subviews] objectAtIndex:0] frame];
    NSRect rightRect = [[[sender subviews] objectAtIndex:1] frame];
    NSRect newFrame  = [sender frame];

    leftRect.size.height = newFrame.size.height;
    leftRect.origin = NSMakePoint(0, 0);
    rightRect.size.width = newFrame.size.width - leftRect.size.width
- dividerThickness;
    rightRect.size.height = newFrame.size.height;
    rightRect.origin.x = leftRect.size.width + dividerThickness;

    [[[sender subviews] objectAtIndex:0] setFrame:leftRect];
    [[[sender subviews] objectAtIndex:1] setFrame:rightRect];
}
0
- (BOOL)splitView:(NSSplitView *)aSplitView shouldAdjustSizeOfSubview:(NSView *)subview
{
    return subview == rightView;
}

add this to your delegate.

if your NSSplitView would contain lefView and rightView, this keeps leftView at a fixed width, and rightView will resize, when resizing the main window.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.