Search code examples
iosuiviewcore-graphicsdrawrectcgpath

DrawRect keeps drawing white background


//inside init
_color = [UIColor orangeColor];
self.backgroundColor = [UIColor clearColor];
self.clearsContextBeforeDrawing = NO;

//inside drawRect
CGContextRef ctx = UIGraphicsGetCurrentContext();

CGContextSaveGState(ctx);
    CGContextAddEllipseInRect(ctx, rect);
    CGContextSetFillColorWithColor(ctx,
                                   _color.CGColor);
    CGContextFillPath(ctx);
CGContextRestoreGState(ctx);

The background keeps showing up as white rather then transparent. The ellipse also does not change color and shows up as black. Thank in advance to anyone who takes a look.


Solution

  • I think you can refer to this answer for your transparency issue : Setting A CGContext Transparent Background

    As for your ellipse color issue, it correctly display as an orange ellipse on my simulator so you may check at possible overlays or other possible side effects from other parts from your code.