I’ve got a custom view with the following drawRect method
- (void)drawRect:(CGRect)rect
{
CGContextRef ctx = UIGraphicsGetCurrentContext();
float w = rect.size.width;
CGContextSetFillColorWithColor(ctx, [UIColor blackColor].CGColor);
CGContextAddArc(ctx, w / 2, w / 2, w / 2 - 10, 0, 2 * M_PI, 0);
CGContextDrawPath(ctx, kCGPathFill);
}
I’m expecting to see a black circle, however, for some reason it draws a black rectangle instead. I guess the whole view is just getting filled with black. Where is the problem?
Perhaps your view’s
backgroundColoris also black (which is the default for opaque views)?Also, you shouldn’t assume that the
rectparameter covers the whole view, it could be only a part of the view that has been marked as needing to be redrawn. You should base your geometry calculations on the view’sboundsinstead.