设置固定圆角,iOS11之后有更简单的方法(适配如下)
特别注意,在iOS10版本下,UITableview通过Masonry约束后直接用下面的方法设置圆角,结果UITableview不显示,可以直接frame赋值
/**
圆角
@param radius 圆角尺寸
@param maskCorner 圆角位置列如:kCALayerMinXMinYCorner|kCALayerMaxXMinYCorner
@param corner 圆角位置列如:UIRectCornerTopRight|UIRectCornerTopLeft
*/
- (void)acs_radiusWithRadius:(CGFloat)radius maskedCorner:(CACornerMask)maskCorner corner:(UIRectCorner)corner {
if (@available(iOS 11.0, *)) {
self.layer.cornerRadius = radius;
self.layer.maskedCorners = maskCorner;
} else {
UIBezierPath * path = [UIBezierPath bezierPathWithRoundedRect:self.bounds byRoundingCorners:corner cornerRadii:CGSizeMake(radius, radius)];
CAShapeLayer *maskLayer = [[CAShapeLayer alloc] init];
maskLayer.frame = self.bounds;
maskLayer.path = path.CGPath;
self.layer.mask = maskLayer;
}
}
UIRectCorner的定义如下:
typedef NS_OPTIONS(NSUInteger, UIRectCorner) {
UIRectCornerTopLeft = 1 << 0,
UIRectCornerTopRight = 1 << 1,
UIRectCornerBottomLeft = 1 << 2,
UIRectCornerBottomRight = 1 << 3,
UIRectCornerAllCorners = ~0UL
};
CACornerMask的定义如下:
typedef NS_OPTIONS (NSUInteger, CACornerMask)
{
kCALayerMinXMinYCorner = 1U << 0, // 左上
kCALayerMaxXMinYCorner = 1U << 1, 右上
kCALayerMinXMaxYCorner = 1U << 2, // 左下
kCALayerMaxXMaxYCorner = 1U << 3, // 右下
};