Re: view drawRect / setNeedsDisplay
Re: view drawRect / setNeedsDisplay
- Subject: Re: view drawRect / setNeedsDisplay
- From: Erik Buck <email@hidden>
- Date: Wed, 20 Jul 2005 22:25:16 -0700 (PDT)
The AppKit effectively sets a flag whenever setNeedsDisplay: is called for a view. The fact that the flag is set causes AppKit to send a drawRect: message to the view and appropriate sub-views at a later time. After the drawRect: is called, the flag is cleared. [This over-simplifies it, but it illustrates the concept.]
If you call setNeedsDisplay: from within drawRect:, the flag will be set, but as soon as drawRect: returns, it will be cleared again. Therefore, when the time arrives, the flag is clear and drawRect: is not called again.
-drawRect should only be used for drawing. Resizing, repositioning, and adding or removing subviews absolutely should not be done in drawRect:. Incorrect behavior will likely result. The AppKit reasonably relies on the view hierarchy remaining constant while it is in the middle of drawing the view hierarchy.
Furthermore, do not call -display or variants or -drawRect from within drawRect: or you invite an infinite recursion and crash.
If you want to rearrange subviews, I recommend that you do it in a method like - (void)setFrame:(NSRect)frameRect or - (void)resizeSubviewsWithOldSize:(NSSize)oldBoundsSize. You might even like NSViewFrameDidChangeNotification.
There is a whole section in Apples NSView documentation on resizing behavior for views and subviews.
Some other NSView subclasses use methods like tile found in NSScrollView.
- (void)tile
Lays out the components of the receiver: the content view, the scrollers, and the ruler views. You rarely need to invoke this method, but subclasses may override it to manage additional components
Perhaps you want to provide a similar method and call it whenever your views frame changes or some other stimulus happens.
_______________________________________________
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