Making UIView transparent

peter1234 picture peter1234 · Mar 15, 2013 · Viewed 39.9k times · Source

I want to add a UIView to my UIViewController but I want it to be transparent until I decide to create an Oval inside the UIView.

Until I create the oval, I can set my view's alpha to 0 but when I want to add the oval, the background color is still black.

here's what it looks like with a bunch of ovals

image

In my UIView:

- (void)drawRect:(CGRect)rect
{
    [self pushContext];
    UIBezierPath *oval = [UIBezierPath bezierPathWithOvalInRect:self.bounds];
    [[UIColor redColor] setFill];
    [oval fill];
    self.opaque = NO;
    [self setBackgroundColor:[UIColor clearColor]];

    [oval addClip];
    [self setBackgroundColor:[UIColor clearColor]];

    [self popContext];
}

Answer

Dharmbir Singh picture Dharmbir Singh · Mar 15, 2013

Please try to use this one

view.backgroundColor = [UIColor colorWithWhite:0.0 alpha:0.0];
view.opaque = NO;