Itzik984 Itzik984 - 1 year ago 114
iOS Question

Drawing UIBezierPath on code generated UIVIew

I have a

added in code at run time.

I want to draw a
in it, but does this means i have to override the
for UIView?

Or is there another way of drawing to it on the custom made

Here is the code for generating the

UIView* shapeView = [[UIView alloc]initWithFrame:CGRectMake(xOrigin,yOrigin+(i*MENU_BLOCK_FRAME_HEIGHT), self.shapeScroll.frame.size.width, MENU_BLOCK_FRAME_HEIGHT)];
shapeView.clipsToBounds = YES;

And here is the function to create and return a

- (UIBezierPath*)createPath
UIBezierPath* path = [[UIBezierPath alloc]init];
[path moveToPoint:CGPointMake(100.0, 50.0)];
[path addLineToPoint:CGPointMake(200.0,50.0)];
[path addLineToPoint:CGPointMake(200.0, 200.0)];
[path addLineToPoint:CGPointMake(100.0, 200.0)];
[path closePath];
return path;


Answer Source

It would be easier if you would use a CAShapeLayer, like this:

CAShapeLayer *shapeView = [[CAShapeLayer alloc] init];

And set its path:

[shapeView setPath:[self createPath].CGPath];

Finally add it:

[[self.view layer] addSublayer:shapeView];
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download