Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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];
like image 646
flatronka Avatar asked Jun 18 '13 06:06

flatronka


People also ask

How UIBezierPath works?

A UIBezierPath object combines the geometry of a path with attributes that describe the path during rendering. You set the geometry and attributes separately and can change them independent of one another. After you have the object configured the way you want it, you can tell it to draw itself in the current context.

What is UIBezierPath in IOS?

The UIBezierPath class is an Objective-C wrapper for the path-related features in the Core Graphics framework. You can use this class to define simple shapes, such as ovals and rectangles, as well as complex shapes that incorporate multiple straight and curved line segments.


1 Answers

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.

like image 133
Lukas Kukacka Avatar answered Nov 10 '22 19:11

Lukas Kukacka