flatronka
flatronka

Reputation: 1081

UIBezierPath simple rectangle

I just want to draw a simple rectangle to a view using the following function:

- (void)drawRect:(CGRect)rect {
    [super drawRect:rect];
    if (self.drawTextBouble) {
        [[UIColor blueColor] setFill];
        UIBezierPath *aPath = [UIBezierPath bezierPathWithRoundedRect:CGRectMake(40, 0, 230, 120) cornerRadius:12.0];
        [aPath fill];
    }
}

The code above fills the view with plain black background, outside the rectangle is not transparent. How can I fix this?

Edit:

The solution below is working, but this is working also:

[self setOpaque:NO];

Upvotes: 7

Views: 9844

Answers (1)

Lukas Kukacka
Lukas Kukacka

Reputation: 7704

You drawing code is OK. If you want the custom drawn view to have transparent background, you just need to set

self.backgroundColor = [UIColor clearColor];

in view's - (id)initWithFrame:(CGRect)frame

Edit: Just a little note regarding calling [super drawRect:rect]. UIView docs says:

If you subclass UIView directly, your implementation of this method does not need to call super. However, if you are subclassing a different view class, you should call super at some point in your implementation.

Upvotes: 9

Related Questions