iOS 设置View的指定圆角
2020-04-21 本文已影响0人
当优秀成为习惯
导入#import <CoreGraphics/CoreGraphics.h>
CGFloat radius = 20;
UIRectCorner corner = UIRectCornerTopRight|UIRectCornerTopLeft; // 右上右左举例
if (@available(iOS 11.0,*)) { // iOS11以上苹果提供了属性
cornerRadiusView.layer.cornerRadius = radius;
cornerRadiusView.layer.maskedCorners = (CACornerMask)corner;
}else{ // iOS11已下用UIBezierPath设置
UIBezierPath *path = [UIBezierPath bezierPathWithRoundedRect:cornerRadiusView.bounds byRoundingCorners:corner cornerRadii:CGSizeMake(radius, radius)];
CAShapeLayer *maskLayer = [[CAShapeLayer alloc]init];
maskLayer.frame = cornerRadiusView.bounds;
maskLayer.path = path.CGPath;
cornerRadiusView.layer.mask = maskLayer;
}