前言
效果图
I 、 iOS设置视图的cornerRadius属性失效的解决方案
1.1 解决步骤
1、尝试设置_numberLab.clipsToBounds = YES;2、尝试设置 [self.numberLab layoutIfNeeded]; 之后再执行cornerRadius
在设置完约束后, 并不能马上得到它的frame, 只要添加[self.view layoutIfNeeded]; 就能拿到frame设置圆角了
- (void)layoutSubviews{ [super layoutSubviews]; [self.numberLab layoutIfNeeded]; [self.contentView bringSubviewToFront:self.numberLab]; self.numberLab.layer.cornerRadius =self.numberLab.height*0.5; }
3、尝试设置_numberLab.layer.masksToBounds = YES;
1.2、masksToBounds属性是什么?它有什么作用
- masksToBounds指在设置子layer在超出父layer时是否被裁剪,YES表示裁剪,NO表示不裁剪,默认是NO;通常在通过设置layer.cornerRadius属性实现圆角效果时要设置masksToBounds为YES,以保证圆角效果的实现,但这种方法是一种很低效的实现方式,也是最简单直接的。
- masksToBounds和clipsToBounds是不同的,前者指子layer层在超出父layer时是否被裁剪(masksToBounds是CALayer的属性),而后者指子view在超出父view时是否被裁剪(clipsToBounds是UIView的属性)。
1.3 只设置顶部的圆角
用法
- (void)layoutSubviews { [super layoutSubviews]; [self.titleV layoutIfNeeded]; [self.titleV setCornerOnTop:kAdjustRatio(20)]; }
setCornerOnTop:方法的实现
#pragma mark - Corner Radius - (void)setCornerOnTop:(CGFloat)radius { UIBezierPath *maskPath; maskPath = [UIBezierPath bezierPathWithRoundedRect:self.bounds byRoundingCorners:(UIRectCornerTopLeft | UIRectCornerTopRight) cornerRadii:CGSizeMake(radius, radius)]; CAShapeLayer *maskLayer = [[CAShapeLayer alloc] init]; maskLayer.frame = self.bounds; maskLayer.path = maskPath.CGPath; self.layer.mask = maskLayer; } - (void)setCornerOnBottom:(CGFloat)radius { UIBezierPath *maskPath; maskPath = [UIBezierPath bezierPathWithRoundedRect:self.bounds byRoundingCorners:(UIRectCornerBottomLeft | UIRectCornerBottomRight) cornerRadii:CGSizeMake(radius, radius)]; CAShapeLayer *maskLayer = [[CAShapeLayer alloc] init]; maskLayer.frame = self.bounds; maskLayer.path = maskPath.CGPath; self.layer.mask = maskLayer; } - (void)setCornerOnLeft:(CGFloat)radius { UIBezierPath *maskPath; maskPath = [UIBezierPath bezierPathWithRoundedRect:self.bounds byRoundingCorners:(UIRectCornerTopLeft | UIRectCornerBottomLeft) cornerRadii:CGSizeMake(radius, radius)]; CAShapeLayer *maskLayer = [[CAShapeLayer alloc] init]; maskLayer.frame = self.bounds; maskLayer.path = maskPath.CGPath; self.layer.mask = maskLayer; } - (void)setCornerOnRight:(CGFloat)radius { UIBezierPath *maskPath; maskPath = [UIBezierPath bezierPathWithRoundedRect:self.bounds byRoundingCorners:(UIRectCornerTopRight | UIRectCornerBottomRight) cornerRadii:CGSizeMake(radius, radius)]; CAShapeLayer *maskLayer = [[CAShapeLayer alloc] init]; maskLayer.frame = self.bounds; maskLayer.path = maskPath.CGPath; self.layer.mask = maskLayer; } - (void)setCornerOnTopLeft:(CGFloat)radius { UIBezierPath *maskPath; maskPath = [UIBezierPath bezierPathWithRoundedRect:self.bounds byRoundingCorners:UIRectCornerTopLeft cornerRadii:CGSizeMake(radius, radius)]; CAShapeLayer *maskLayer = [[CAShapeLayer alloc] init]; maskLayer.frame = self.bounds; maskLayer.path = maskPath.CGPath; self.layer.mask = maskLayer; } - (void)setCornerOnTopRight:(CGFloat)radius { UIBezierPath *maskPath; maskPath = [UIBezierPath bezierPathWithRoundedRect:self.bounds byRoundingCorners:UIRectCornerTopRight cornerRadii:CGSizeMake(radius, radius)]; CAShapeLayer *maskLayer = [[CAShapeLayer alloc] init]; maskLayer.frame = self.bounds; maskLayer.path = maskPath.CGPath; self.layer.mask = maskLayer; } - (void)setCornerOnBottomLeft:(CGFloat)radius { UIBezierPath *maskPath; maskPath = [UIBezierPath bezierPathWithRoundedRect:self.bounds byRoundingCorners:UIRectCornerBottomLeft cornerRadii:CGSizeMake(radius, radius)]; CAShapeLayer *maskLayer = [[CAShapeLayer alloc] init]; maskLayer.frame = self.bounds; maskLayer.path = maskPath.CGPath; self.layer.mask = maskLayer; } - (void)setCornerOnBottomRight:(CGFloat)radius { UIBezierPath *maskPath; maskPath = [UIBezierPath bezierPathWithRoundedRect:self.bounds byRoundingCorners:UIRectCornerBottomRight cornerRadii:CGSizeMake(radius, radius)]; CAShapeLayer *maskLayer = [[CAShapeLayer alloc] init]; maskLayer.frame = self.bounds; maskLayer.path = maskPath.CGPath; self.layer.mask = maskLayer; } - (void)setAllCorner:(CGFloat)radius { UIBezierPath *maskPath; maskPath = [UIBezierPath bezierPathWithRoundedRect:self.bounds cornerRadius:radius]; CAShapeLayer *maskLayer = [[CAShapeLayer alloc] init]; maskLayer.frame = self.bounds; maskLayer.path = maskPath.CGPath; self.layer.mask = maskLayer; } - (void)setCornerRadius:(CGFloat)radius { [self.layer setCornerRadius:radius]; [self.layer setMasksToBounds:YES]; }
see also
iOS去掉TabBar的顶部黑线,并添加发光的阴影

