专注iOS开发(OC/Swift)那些年!那些事!

iOS-圆角和边框共存

2019-04-17  本文已影响16人  洲洲哥

圆角和边框共存

CGRect rect = CGRectMake(10, 100, 300, 200);
    UIView * view = [[UIView alloc] initWithFrame:rect];
    view.backgroundColor = [UIColor grayColor];
//    view.layer.borderWidth = 1;
//    view.layer.borderColor = [UIColor greenColor].CGColor;
    [self.view addSubview:view];

    UIBezierPath *rounded = [UIBezierPath bezierPathWithRoundedRect:view.bounds byRoundingCorners:(UIRectCornerTopRight | UIRectCornerTopLeft) cornerRadii:CGSizeMake(5, 5)];
    CAShapeLayer *shapLayer = [[CAShapeLayer alloc] init];
    [shapLayer setPath:rounded.CGPath];
    view.layer.mask = shapLayer;
    
    CAShapeLayer *borderLayer = [CAShapeLayer layer];
    borderLayer.frame = view.bounds;
    borderLayer.lineWidth = 1.5f;
    borderLayer.strokeColor = [UIColor  redColor].CGColor;
    borderLayer.fillColor = [UIColor clearColor].CGColor;
    borderLayer.path = rounded.CGPath;

    [view.layer insertSublayer:borderLayer atIndex:0];

可以来微信公众号(洲洲哥)后台给我留言。 快来扫码关注我们吧!

公众号二维码
上一篇下一篇

猜你喜欢

热点阅读