iOS view虚线边框

iOS view虚线边框_第1张图片
Paste_Image.png

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    
    UIView *view = [[UIView alloc] init];
    [self.view addSubview:view];
    view.backgroundColor = [UIColor redColor];
    view.frame = CGRectMake(100, 100, 100, 100);
    
    [self addBorderToLayer:view];
}

- (void)addBorderToLayer:(UIView *)view
{
    CAShapeLayer *border = [CAShapeLayer layer];
    
    border.strokeColor = [UIColor blackColor].CGColor;
    
    border.fillColor = nil;
    
    border.path = [UIBezierPath bezierPathWithRect:view.bounds].CGPath;
    
    border.frame = view.bounds;
    
    border.lineWidth = 2;
    
    border.lineCap = @"square";
    
    border.lineDashPattern = @[@16, @7];
    
    [view.layer addSublayer:border];
}

你可能感兴趣的:(iOS view虚线边框)