我有一个类别中的以下代码进行四舍五入.我也想画边界.但边角没有显示在角落的圆角上.
这是代码
- (void) roundTopCorners:(CGFloat) radius { self.layer.masksToBounds = YES; CGRect bounds = self.bounds; UIBezierPath *maskPath = [UIBezierPath bezierPathWithRoundedRect:bounds byRoundingCorners:(UIRectCornerTopLeft | UIRectCornerTopRight) cornerRadii:CGSizeMake(radius,radius)]; CAShapeLayer *maskLayer = [CAShapeLayer layer]; maskLayer.frame = bounds; maskLayer.path = maskPath.CGPath; maskLayer.strokeColor = [UIColor redColor].CGColor; self.layer.mask = maskLayer; }
解决方法
掩模层不被绘制,仅用于计算掩模.尝试:
-(void)roundCorners:(UIRectCorner)corners radius:(CGFloat)radius { CGRect bounds = self.bounds; UIBezierPath *maskPath = [UIBezierPath bezierPathWithRoundedRect:bounds byRoundingCorners:corners cornerRadii:CGSizeMake(radius,radius)]; CAShapeLayer *maskLayer = [CAShapeLayer layer]; maskLayer.frame = bounds; maskLayer.path = maskPath.CGPath; self.layer.mask = maskLayer; CAShapeLayer* frameLayer = [CAShapeLayer layer]; frameLayer.frame = bounds; frameLayer.path = maskPath.CGPath; frameLayer.strokeColor = [UIColor redColor].CGColor; frameLayer.fillColor = nil; [self.layer addSublayer:frameLayer]; } -(void)roundTopCornersRadius:(CGFloat)radius { [self roundCorners:(UIRectCornerTopLeft|UIRectCornerTopRight) radius:radius]; } -(void)roundBottomCornersRadius:(CGFloat)radius { [self roundCorners:(UIRectCornerBottomLeft|UIRectCornerBottomRight) radius:radius]; }
您当前看到的框架是UITextField的正常框架,因此将框架样式设置为none.您还必须调整插图来弥补将框架样式设置为无,通常没有插入的事实.