-1

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?

John Topley
  • 113,588
  • 46
  • 195
  • 237
JimmyYXA
  • 71
  • 6

1 Answers1

1

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.

jrturton
  • 118,105
  • 32
  • 252
  • 268