Re: Strange stretching NSView
Re: Strange stretching NSView
- Subject: Re: Strange stretching NSView
- From: Gonzalo Castro <email@hidden>
- Date: Thu, 01 Feb 2007 16:26:40 -0500
- Thread-topic: Strange stretching NSView
On 2/1/07 3:01 PM, "email@hidden"
<email@hidden> wrote:
> Message: 5
> Date: Thu, 1 Feb 2007 14:38:36 -0500
> From: Ken Tozier <email@hidden>
> Subject: Strange stretching NSView
> To: Cocoa Dev List <email@hidden>
> Message-ID: <email@hidden>
> Content-Type: text/plain; charset=US-ASCII; delsp=yes; format=flowed
>
> Hi
>
> I'm sure this is just a coordinate problem but I've been trying for
> about four hours and can't figure it out. Basically, I'm creating a
> custom view that wraps it's content views (similar to the iPhoto
> thumbnail view) and what's happening is that the wrapping view itself
> is more than twice as deep as I tell it to be and all subviews are
> rendering with incorrect frame sizes. I tell them to size to 27 x 34
> pixels and they are displaying at 23 x 84 pixels.
>
> I've tried each of the following with no success
>
> - set the "isFlipped" to return both YES and NO
> - set the setAutoresizingMask to NSViewWidthSizable for the
> containing view and NSViewNotSizable for the subviews
> - query container and subviews for their size at runtine (all values
> correct)
> - checked frame values to make sure I wasn't flopping width for
> height and vice versa
>
> I'm stumped. Anyone have an idea why views would stretch despite the
> fact that, when queried for their size at runtime, all views return
> correct values?
Hi dude! No quick answers but some observations below.
>
> Any help appreciated
>
> Ken
>
> Here's the wrapping view initializer
>
> - (id) initWithFrame:(NSRect) frame
> {
> self = [super initWithFrame: frame];
> if (self)
> {
> // Initialization code here.
> indent = 10;
> horizSpacing = 5;
> vertSpacing = 8;
> pages = [[NSMutableDictionary alloc] init];
> pageWidth = [KPage defaultWidth];
> pageHeight = [KPage defaultHeight];
> maxHeight = 2 * indent + 3 * vertSpacing + 4 * pageHeight;
> pageCount = 0;
>
> [self updateGeometry];
>
> [self setAutoresizingMask: NSViewWidthSizable];
> }
>
> return self;
> }
>
> Here's the wrapping view "updateGeometry" method:
>
> - (void) updateGeometry
> {
> int width = [self bounds].size.width,
> remainder;
>
> pageCount = [pageRecords count];
> usableWidth = width - 2 * indent;
> pagesPerRow = (usableWidth - horizSpacing) / (pageWidth +
> horizSpacing);
> remainder = ((pageCount % pagesPerRow) > 0) ? 1 : 0;
> rowCount = (pageCount / pagesPerRow) + remainder;
> totalHeight = pageHeight * rowCount + vertSpacing * (rowCount - 1)
> + 2 * indent;
> rightMax = width - indent;
> }
>
> Here's the relevant part of the wrapping view "drawRect" method:
>
> if (recordsChanged == YES)
> {
> NSRect pageFrame;
>
> NSEnumerator *enumerator = [pageRecords objectEnumerator];
> NSDictionary *pageRecord;
> KPage *page;
> NSNumber *key;
>
> int top = indent,
> left = indent;
>
> // delete all the old pages
> [pages removeAllObjects];
>
> // add the new pages
> while (pageRecord = [enumerator nextObject])
> {
> // check for right edge
> if ((left + horizSpacing + pageWidth) > rightMax)
> {
> // create a new row
> left = indent;
> top += (pageHeight + vertSpacing);
> }
>
> pageFrame = NSMakeRect(left, top, pageWidth, pageHeight);
>
> // get the page record ID
> key = [pageRecord objectForKey: @"id"];
>
> // create the page
> page = [KPage pageWithFrame: pageFrame record: pageRecord];
>
> // add page to page list
> [pages setObject: page forKey: key];
>
It seems to me that you shouldn't be adding subviews to a view your
currently drawing into. I think this could be the reason your views aren't
stretching the way you expect.
Also, "KPage pageWithFrame" looks like stuff you should be doing in your
Data classes (MVC) not in your drawing routines. You drawRect shouldn't be
changing (adding/removing) the data it's drawing. The data should change
somewhere else and your if (recordsChanged == YES) test should tell the view
to redraw itself with setNeedsDisplay:YES from you data or controller
classes.
Sorry no concrete answers just hunches. Good luck!
> // add page to view
> [self addSubview: page];
>
> // update variables for next pass
> left += (pageWidth + horizSpacing);
> }
>
> // reset recordsChanged
> recordsChanged = NO;
> }
>
> And here's the KPage initializer:
>
> - (id) initWithFrame:(NSRect) frame
> record:(NSDictionary *) inPageRecord
> {
> self = [super initWithFrame: frame];
> if (self)
> {
> // set properties
> record = [inPageRecord retain];
> page = [[record objectForKey: @"page_number"] intValue];
> oddPage = [self isOddPage: page];
> active = YES;
>
> // set sizing constraints
> [self setAutoresizingMask: NSViewNotSizable];
>
> // add elements
> pageField = [[KPageNumberField alloc] initWithFrame: [self bounds]];
> [self addSubview: pageField];
>
> [self setPageNumber: page];
> [self updateImages];
>
> currentImage = [self imageForKey: KViewStateInactive];
> }
>
> return self;
> }
_______________________________________________
Cocoa-dev mailing list (email@hidden)
Do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden