I just want to draw a rect on a view. This is my code in my UIView subclass:
- (void)drawRect:(CGRect)rect
{
context = UIGraphicsGetCurrentContext();
CGContextSetRGBFillColor(context, 255.0/255.0, 0.0/255.0, 0.0/255.0, 1);
CGContextAddRect(context, (CGRectMake(20, 20, 20, 20)));
}
When I run it no rect is drawn. What's wrong?
CGContextAddRect
just adds a rectangular path to the context. You need to stroke or fill the path as well using CGContextFillPath
or CGContextStrokePath
.
You can also fill a rect directly with UIRectFill
or CGContextFillRect
.