Reputation: 6605
I have a UIView and drawing its content from drawRect()
, but I need to resize the view sometimes depending on some calculation that are done during the drawing:
When I change the size, the drawing seams to scale and not to use the additional size gained from the resize.
This is how I'm resizing form drawRect()
CGRect limits = self.frame ;
limits.size.width = 400;
self.frame = limits;
It seams like the context is not aware of the resize and is drawing using the old sizes. I already try [self setNeedsDisplay]
after resizing but the draw method is not called again.
Upvotes: 1
Views: 1141
Reputation: 4622
For anybody struggling with that.
Apparently [self setNeedsDisplay] won't have any effect if called from drawRect: (or any method called from drawRect:). Somewhy it just skips the call.
To make things happen use following instead
[self performSelector:@selector(setNeedsDisplay) withObject:nil afterDelay:0];
This will be called right when main queue is free again; just make sure to use some condition for calling needsDisplay inside of drawRect - or else you're going to get looped.
Upvotes: 0
Reputation: 5431
Calculations should be done ahead of time and not during drawing, for performance reasons alone (though early calculations would also avoid this problem).
For example, if your calculations depend on factors X
, Y
and Z
, arrange to redo those calculations whenever there is a change to X
, Y
or Z
. Cache the results of the calculation if necessary, and resize your view frame at that time. The drawRect:
implementation should only draw into the area it is given.
Upvotes: 1