Resize NSSplitView Nicely


/ Published in: Objective C
Save to your folder(s)

This provides a nice split view resizing method with a minimum size restraint. You HAVE to specify MIN_LEFT_PANE_W for this to work.


Copy this code and paste it in your HTML
  1. - (void) splitView:(NSSplitView *)sender resizeSubviewsWithOldSize:(NSSize)oldSize {
  2. // http://www.wodeveloper.com/omniLists/macosx-dev/2003/May/msg00261.html
  3.  
  4. // grab the splitviews
  5. NSView *left = [[sender subviews] objectAtIndex:0];
  6. NSView *right = [[sender subviews] objectAtIndex:1];
  7.  
  8. float dividerThickness = [sender dividerThickness];
  9.  
  10. // get the different frames
  11. NSRect newFrame = [sender frame];
  12. NSRect leftFrame = [left frame];
  13. NSRect rightFrame = [right frame];
  14.  
  15. // change in width for this redraw
  16. int dWidth = newFrame.size.width - oldSize.width;
  17.  
  18. // ratio of the left frame width to the right used for resize speed when both panes are being resized
  19. float rLeftRight = (leftFrame.size.width - MIN_LEFT_PANEL_W) / rightFrame.size.width;
  20.  
  21. // resize the height of the left
  22. leftFrame.size.height = newFrame.size.height;
  23. leftFrame.origin = NSMakePoint(0,0);
  24.  
  25. // resize the left & right pane equally if we are shrinking the frame
  26. // resize the right pane only if we are increasing the frame
  27. // when resizing lock at minimum width for the left panel
  28. if(leftFrame.size.width <= MIN_LEFT_PANEL_W && dWidth < 0) {
  29. rightFrame.size.width += dWidth;
  30. } else if(dWidth > 0) {
  31. rightFrame.size.width += dWidth;
  32. } else {
  33. leftFrame.size.width += dWidth * rLeftRight;
  34. rightFrame.size.width += dWidth * (1 - rLeftRight);
  35. }
  36.  
  37. rightFrame.size.width = newFrame.size.width - leftFrame.size.width - dividerThickness;
  38. rightFrame.size.height = newFrame.size.height;
  39. rightFrame.origin.x = leftFrame.size.width + dividerThickness;
  40.  
  41. [left setFrame:leftFrame];
  42. [right setFrame:rightFrame];
  43. }

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.