361Transparent Background of Custom Drawing Class

Usually common problems already have simple solution. Like that one:

Problem You subclassed UIView, you want to do some custom drawing in drawRect, but no matter what you do or where you draw, the background of the view remains black.

- (void)drawRect:(CGRect)rect {
 // Drawing code
 CGContextRef context = UIGraphicsGetCurrentContext();
 CGContextSetRGBFillColor(context, 0.0, 0.0, 1.0, 1.0);
 CGContextFillEllipseInRect(context, rect);
}

Solution In the ViewController, which call the drawing class, add

myDrawingClass.opaque = NO;
  • or even nicer. In the drawing class' init function:
    self.opaque = NO;

And not like that

  • adding "self.opaque = NO;" in the drawRect: function
  • CGContextClearRect(context, rect);
  • CGContextSetAlpha(context, 0.5f);