CAShapeLayer used in UIView subclass doesn't work

i tried a few hours to get a dotted border around my UIView with CAShapeLayer but i don’t get it displayed.

#import <UIKit/UIKit.h>
#import <QuartzCore/QuartzCore.h>

@interface ScaleOverlay : UIView <UIGestureRecognizerDelegate> {
    CAShapeLayer *shapeLayer_;


  • (viewConroller.view removeFromSuperview) Thread:1 EXC_BAD_ACCESS (Code=1, address = 0x6000000008)
  • What exactly is a Private API, and why will Apple reject an iOS App if one is used?
  • IOS: send a mail without iphone/ipad mail app
  • UIPercentDrivenInteractiveTransition doesn't get to animation's completion on fast gesture
  • APNs duplicate tokens
  • How to convert from YUV to CIImage for iOS
  • - (id)initWithFrame:(CGRect)frame
    self = [super initWithFrame:frame];
    if (self) {
        self.backgroundColor = [UIColor redColor];
        self.alpha = 0;
        //Round corners
        [[self layer] setCornerRadius:8.f];
        [[self layer] setMasksToBounds:YES];
        shapeLayer_ = [[CAShapeLayer layer] retain];
        CGMutablePathRef path = CGPathCreateMutable();
        CGPathAddRect(path, NULL, frame);
        shapeLayer_.path = path;
        shapeLayer_.backgroundColor = [[UIColor clearColor] CGColor];
        shapeLayer_.frame = frame;
        shapeLayer_.position =;
        [shapeLayer_ setValue:[NSNumber numberWithBool:NO] forKey:@"isCircle"];
        shapeLayer_.fillColor = [[UIColor blueColor] CGColor];
        shapeLayer_.strokeColor = [[UIColor blackColor] CGColor];
        shapeLayer_.lineWidth = 4.;
        shapeLayer_.lineDashPattern = [NSArray arrayWithObjects:[NSNumber numberWithInt:8], [NSNumber numberWithInt:8], nil];
        shapeLayer_.lineCap = kCALineCapRound;
    return self;

    I got a red rect drawn in my Superview but not the border. Copied this from a source example in the hope it would work, but it doesn’t.

    Solutions Collect From Internet About “CAShapeLayer used in UIView subclass doesn't work”

    You never add shapeLayer as a sublayer of your UIView’s layer, so it’s never displayed onscreen. Try adding

    [self.layer addSublayer:shapeLayer_];

    after you set up your CAShapeLayer in your -initWithFrame: method.

    Even better, you could try making your UIView’s backing layer a CAShapeLayer by overriding the following class method:

    + (Class) layerClass 
        return [CAShapeLayer class];

    You could then deal with the view’s layer directly, and eliminate the additional CAShapeLayer instance variable.