5

I am playing around with NSSplitView - quite successfully for now but here is my Problem:

My SplitView looks like this: SplitView

Test project here: https://www.dropbox.com/s/amz863l11nvkdir/TestNSSplitView.zip

I've implemented - (void)splitView:(NSSplitView *)splitView resizeSubviewsWithOldSize:(NSSize)oldSize to stick the left and the right subview at the same size as before while resizing.

If I open the Window which contains the NSSplitView this message comes up in the console:

<NSSplitView: 0x107de1520>: the delegate <BRSchematicWindowController: 0x10ac11050> was sent -splitView:resizeSubviewsWithOldSize: and left the subview frames in an inconsistent state:
Split view bounds: {{0, 0}, {1068, 600}}
     Subview frame: {{0, 0}, {182, 600}}
     Subview frame: {{183, 0}, {640, 600}}
     Subview frame: {{824, 0}, {243, 600}}
The outer edges of the subview frames are supposed to line up with the split view's bounds' edges. NSSplitView is working around the problem, perhaps at the cost of more redrawing. (This message is only logged once per NSSplitView.)

What is wrong here? I didn't get it even after reading this message...

PS: In the right splitView there is another NSSplitView this isn't the failure. I get this message even without this additional NSSplitView.

tuna
  • 931
  • 2
  • 10
  • 28
  • +1 for the test project ;) – HAS Apr 28 '13 at 20:50
  • I don't like to say that because in 99.999999 % of all cases it's wrong but I think it's a bug. Even if you call the method without anything in it it logs the message ... – HAS Apr 28 '13 at 21:14
  • hmm that didn't satisfies me. If I investigated correctly then this is the only place where I can do this. – tuna Apr 28 '13 at 21:22
  • When you resize the window, is the desired behavior that the subview on the far left and the one on the far right remain the same width, and only the subview in the middle gets wider or narrower? – NSGod Apr 28 '13 at 22:17
  • What's the minimum version of OS X you want to support? 10.6 and greater, or do you need 10.5 support? – NSGod Apr 28 '13 at 22:19
  • 10.6 and greater @NSGod. I currently watching WWDC 232 - Autolayout by Example. Maybe this can be my solution. – tuna Apr 28 '13 at 22:20

1 Answers1

8

If you're targeting OS X 10.6+, then it's much easier to use NSSplitView's splitView:shouldAdjustSizeOfSubview: to control sizing.

One way to use this method would be to add the following IBOutlets to your .h file:

@property (nonatomic, weak) IBOutlet NSView *leftView;
@property (nonatomic, weak) IBOutlet NSView *centerView;
@property (nonatomic, weak) IBOutlet NSView *rightView;

Then implement splitView:shouldAdjustSizeOfSubview: in your .m file like this:

- (BOOL)splitView:(NSSplitView *)aSplitView
             shouldAdjustSizeOfSubview:(NSView *)subview {
    return (subview == _centerView);
    // or return !(subview == _leftView || subview == _rightView);
}

By implementing that, you basically say "only adjust the size of center subview when resizing".

You can comment out the entire splitView:resizeSubviewsWithOldSize: method for the time being unless you need to customize the default behavior in some way.

Note that if you have multiple split views that have this controller object set to be their delegate, you may want to check to see which split view is being passed in the aSplitView parameter and do different things accordingly.

NSGod
  • 22,699
  • 3
  • 58
  • 66
  • Wow! I've been struggling with resizeSubviewsWithOldSize for a very long time and I didn't even know about this method. I should have read the docs more closely. This is WAY simpler than all the other crazy implementations of resizeSubviewsWithOldSize. – Tap Forms Apr 26 '14 at 19:19
  • 1
    Awesome answer. BTW if you know the index of the subview that you want to allow to resize, you don't need to make outlets, you can just say `return subview == [aSplitView subviews][1];` – jsd May 03 '14 at 16:49
  • I wish I could give you a hundred points for this answer man! You totally made my day!! jsd's comment makes it a wrap. – Fabio Russo Nov 10 '14 at 16:07
  • Dude you just cured a 6 hour headache! – d00dle Jan 18 '15 at 20:43