views:

160

answers:

1

I've worked for hours on this and I just can't get it - should be really basic stuff but it's not getting thru my thick skull. I think it has to do with the coordinate system in Cocoa but I really don't know.

This is all happening in the top pane of a horizontal NSSplitView.

Very simply, I'm trying to position one NSBox right below a second one (I load custom views into the boxes - that all works fine). The top box's top-left corner is at the top-left corner of the pane and never changes. If the height of the top NSBox shrinks I want the top of the second NSBox to slide right up below it. Conversely, if the top NSBox's height increases I want the bottom NSBox to slide down.

This code gets called twice. box is correct (first time top box, second time bottom box) and v is correct (this is the view I'm loading into the box - this works fine and it is what is causing the height to change in the top box).

Thanks

NSSize destBoxSize = [[box contentView] frame].size; //the size of the box in the view to load the view into NSSize newViewSize = [v frame].size; // the size of the view to be loaded

float deltaWidth = [horizSplitView frame].size.width - destBoxSize.width; float deltaHeight = newViewSize.height - destBoxSize.height; NSRect boxFrame = [box frame]; boxFrame.size.height += deltaHeight; boxFrame.size.width += deltaWidth; boxFrame.origin.y -= deltaHeight;

NSLog(@"vc=%@ boxFrame x%f y%f h%f w%f", nibName, boxFrame.origin.x, boxFrame.origin.y, boxFrame.size.height, boxFrame.size.width);

// Clear the box for resizing [box setContentView:nil]; [box setContentView:v]; [box setFrame:boxFrame];

A: 

What you want to do is not so hard, but it will need some subclassing. First of all, you need to subclass NSSplitView and either and override either -(void)init or -(void)awakeFromNib to add this line:

[self setAutoresizesSubviews:YES];  //

Then you need to subclass the two boxes and set their auto resizing masks, either in -(void)init or in - (void)viewWillMoveToSuperview:(NSView *)newSuperView. For the first box you'll probably want:

[newInstance setAutoresizingMask:NSViewNotSizable];

For the second bbox you'll probably want:

[newInstance setAutoresizingMask:NSViewMinXMargin | NSViewMinYMargin];

See also NSView. It takes a bit of experimenting to get the right combination, but then it works quite nicely.

Elise van Looij