经常有这样的需求,给view添加圆角,目前用过三种实现方式,第四种我也没用过。圆角总容易伴随着离屏渲染的问题
1 、设置layer.cornerRadius属性
view.layer.cornerRadius = 50;
如果是给一个view 这种content为空的视图设置圆角,上面就达到了效果,如果是给UILabel 或者 UIButton 或者给CollectionView的一个item,contents不为空的视图设置圆角就不管用,需要两行代码共同使用
lab.layer.cornerRadius = 15;
lab.layer.masksToBounds = YES;
2、直接使用layer
CAShapeLayer *layer = [CAShapeLayer layer];
layer.fillColor = [UIColor blueColor].CGColor;
layer.frame = CGRectMake(10, 500, 100, 100);
[self.view.layer addSublayer:layer];
UIBezierPath *path = [UIBezierPath bezierPathWithRoundedRect:layer.bounds byRoundingCorners:UIRectCornerTopLeft | UIRectCornerTopRight cornerRadii:CGSizeMake(12, 12)];
layer.path = path.CGPath;
3、添加mask
UIView *v2 = [[UIView alloc]