Why does setFrame: change the origin of the bounds?
Why does setFrame: change the origin of the bounds?
- Subject: Why does setFrame: change the origin of the bounds?
- From: James Bucanek <email@hidden>
- Date: Fri, 12 May 2006 10:40:34 -0700
Greetings,
I'm struggling with NSView coordinate systems (again). I can't find this documented anywhere, so I'm hoping someone can explain it to me.
I have an NSBox that is populated with a variable number of controls. I want to resize the NSBox to exactly encompass the subviews whenever the number or placement of the subviews change.
My approach starts by calculating a rectangle that encompasses all of the subviews. It then shifts all of the subviews as a group so that this enclosing rectangle touches the origin. I then resize the NSBox using -[NSBox setFrameFromContentFrame:].
However, when I do this the origin of the bounds of the content view of the NSBox changes. Basically, however much I adjust the height of the NSBox, the origin of every subview is shifted by that amount.
Let's say I start out with an NSBox that was 500 pixels wide and 48 pixels high. I position an NSButton at {0,0} and decide that the NSBox only needs to be 30 pixels high. When I change the height of the content frame to 30 pixels, the NSButton is suddenly moved to corrodinates {0,-18}.
Shouldn't the relative origin of the bounds of an NSView remain the same at all times? Shouldn't a subview at {10,20} stay at {10,20} regardless of what happens to the frame of that NSView?
- I was wondering if the autoresize flags were getting me. I set them to every possible combination of vertical resize flags and it made no difference.
- I experimented with moving the y origin of the content view's rect so that I was moving the "other" horizontal edge, but it didn't change anything.
Here's the code:
- (void)resizePartView
{
NSBox* boxView = [self boxView];
NSView* contentView = [boxView contentView];
NSRect originalContentFrame = [contentView frame];
//
// Snap all of the subviews to the origin
//
// Calculate a rectangle that encompasses all of the subviews
NSRect lassoRect = NSZeroRect;
NSEnumerator* e = [[contentView subviews] objectEnumerator];
NSView* subview;
while ( (subview=[e nextObject]) != nil )
lassoRect = NSUnionRect(lassoRect,[subview frame]);
// Expand that rect so that is has a little margin
lassoRect = NSInsetRect(lassoRect,-16.0,-8.0);
// Move all of the subviews towards the origin by the offset of lassoRect
// (The origin of lassoRect is, by definition, its offset from the origin)
if (lassoRect.origin.x!=0.0 || lassoRect.origin.y!=0.0)
{
// Move all of the subview by that amount
e = [[contentView subviews] objectEnumerator];
while ( (subview=[e nextObject]) != nil )
[subview setFrame:NSOffsetRect([subview frame],-lassoRect.origin.x,-lassoRect.origin.y)];
[contentView setNeedsDisplay:YES];
}
// Adjust the size of the box's content view so that it is the exact height of its contents
float newHeight = NSHeight(lassoRect);
if (originalContentFrame.size.height!=newHeight)
{
//originalContentFrame.origin.y += newHeight-originalContentFrame.size.height;
originalContentFrame.size.height = newHeight;
[boxView setFrameFromContentFrame:[boxView convertRect:originalContentFrame toView:[boxView superview]]];
[boxView setNeedsDisplay:YES];
}
}
--
James Bucanek
_______________________________________________
Do not post admin requests to the list. They will be ignored.
Cocoa-dev mailing list (email@hidden)
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden