Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

When is 'drawRect' called?

I have some custom drawing code in drawRect which also performs some calculation of sizes.

When is the earliest I can be sure that this code has been loaded, e.g. if I want to modify it's containers size accordingly?

like image 980
Egil Avatar asked Mar 15 '10 21:03

Egil


2 Answers

I have some custom drawing code in drawRect which also performs some calculation of sizes.

When is the earliest I can be sure that this code has been loaded, e.g. if I want to modify it's containers size accordingly?

An object can't exist until its class is fully loaded. If you have an instance, the class that it's an instance of is completely loaded, because you wouldn't have an instance of it if it wasn't.

As for when it's called: It's called when you need to draw. This normally happens as part of the event loop, if anything has marked the view as needing display. It is possible to directly tell an NSView to display, but, as far as I can tell, this is not possible for UIViews.

So, if you need to do something to it before it gets told to, either do it immediately after creating it or, if you're about to set the view as needing display, do it before you do that.

like image 39
Peter Hosey Avatar answered Oct 15 '22 19:10

Peter Hosey


-[NSView viewWillDraw] is a reasonable place for last minute layout.

like image 166
Ken Avatar answered Oct 15 '22 20:10

Ken