Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Making UIView transparent

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];
}
like image 785
peter1234 Avatar asked Mar 15 '13 05:03

peter1234


People also ask

How to make UIView transparent in iOS Swift?

Any SwiftUI view can be partially or wholly transparent using the opacity() modifier. This accepts a value between 0 (completely invisible) and 1 (fully opaque), just like the alpha property of UIView in UIKit.

How do I make the background transparent in Xcode?

xib, you simply do it in interface builder by selecting the option "Clear Color" for the Background of the view in the Utilities Pane (the pane on the right). "Clear Color" will give the view a completely transparent background.


2 Answers

Please try to use this one

view.backgroundColor = [UIColor colorWithWhite:0.0 alpha:0.0];
view.opaque = NO;
like image 140
Dharmbir Singh Avatar answered Nov 02 '22 03:11

Dharmbir Singh


- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        // Initialization code

        self.opaque = NO;

    }
    return self;
}



- (void)drawRect:(CGRect)rect
{
    [[UIColor colorWithRed:1.0 green:1.0 blue:1.0 alpha:0.0] setFill];
    UIRectFill(rect);
    [self pushContext];
    UIBezierPath *oval = [UIBezierPath bezierPathWithOvalInRect:self.bounds];
    [[UIColor redColor] setFill];
    [oval fill];

    [oval addClip];

    [self popContext];
}
like image 36
Guo Luchuan Avatar answered Nov 02 '22 02:11

Guo Luchuan