auto-sizing container view
auto-sizing container view
- Subject: auto-sizing container view
- From: Todd Ransom <email@hidden>
- Date: Fri, 17 Jun 2005 13:22:57 -0600
Hello,
My application requires a view sort of like the paginated view seen
in many word processors. It contains a series of custom views
arranged from top to bottom within a scroll view. Each of these
custom views automatically sizes itself vertically to fit text
entered into its box. The container view should automatically resize
itself to contain all of the custom views and make sure they are
positioned properly.
It does this by listening for frame changed notifications from its
subviews and adjusting its own height accordingly, then positioning
each of the subviews accordingly.
The problem is that when a subview grows it is not being positioned
properly. If a subview grows vertically its Y origin drops (both the
views and the container view return YES from isFlipped) and it
intrudes on the subview below it. So just by entering a bunch of
carriage returns in a box it will drop down the page farther and
farther into the subview below it. But as soon as I shrink the
subview by backspacing over its last line everything pops into position.
The relevant code from the container view is below. Any help or
advice is much appreciated.
thanks,
TR
- (void) frameChanged: (NSNotification *) aNotification {
id changedView = [aNotification object];
if ([changedView respondsToSelector:@selector(oldFrame)]) {
if (!NSEqualSizes([changedView frame].size, [changedView
oldFrame].size)) {
[self fitSubviews];
}
}
}
- (void)fitSubviews {
NSArray *subviews = [self subviews];
NSSize mySize = NSZeroSize;
unsigned int i, count = [subviews count];
float containerHeight = 0.0;
id thisView;
// get the total height for my frame
for (i = 0; i < count; i++) {
thisView = [[subviews objectAtIndex:i] retain];
containerHeight += marginSize;
containerHeight += [thisView frame].size.height;
[thisView release];
}
mySize.width = [[self superview] bounds].size.width;
mySize.height = containerHeight;
[self setFrameSize: mySize];
[self setNeedsDisplay: YES];
for (i = 0; i < count; i++) {
thisView = [[subviews objectAtIndex:i] retain];
NSRect thisViewRect = [thisView frame];
thisViewRect.origin.y = marginSize;
NSLog(@"positioning view number %i", i);
if (i > 0) {
NSRect lastViewRect = [[subviews objectAtIndex: i-1]
frame];
thisViewRect.origin.y += NSMaxY(lastViewRect);
}
NSLog(@"thisViewRect.origin.y = %f", thisViewRect.origin.y);
NSLog(@"thisViewRect.size.height = %f",
thisViewRect.size.height);
NSLog(@"thisViewRect.maxY = %f", NSMaxY(thisViewRect));
[self setNeedsDisplayInRect: [thisView frame]];
[thisView setFrameOrigin: thisViewRect.origin];
[thisView setNeedsDisplay: YES];
[thisView release];
}
[self setNeedsDisplay: YES];
}
_______________________________________________
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