圆angularUIView的顶部和添加边框

我在下面的代码中做了四舍五入的分类。 我也想画一个边框。 但边angular在angular落的圆angular部分没有显示。

在这里输入图像说明

这是代码

- (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的正常框架,因此将框架样式设置为无。 您还必须调整插页以弥补事实,即将框架样式设置为“无”,通常不会有插入。

大卫·贝瑞的迅捷版答案:

 func roundCorners(corners:UIRectCorner, radius:CGFloat) { let bounds = self.bounds let maskPath = UIBezierPath(roundedRect: bounds, byRoundingCorners: corners, cornerRadii: CGSizeMake(radius, radius)) let maskLayer = CAShapeLayer() maskLayer.frame = bounds maskLayer.path = maskPath.CGPath self.layer.mask = maskLayer let frameLayer = CAShapeLayer() frameLayer.frame = bounds frameLayer.path = maskPath.CGPath frameLayer.strokeColor = UIColor.redColor().CGColor frameLayer.fillColor = nil self.layer.addSublayer(frameLayer) } func roundTopCornersRadius(radius:CGFloat) { self.roundCorners([UIRectCorner.TopLeft, UIRectCorner.TopRight], radius:radius) } func roundBottomCornersRadius(radius:CGFloat) { self.roundCorners([UIRectCorner.BottomLeft, UIRectCorner.BottomRight], radius:radius) } 

这可能是一个非常晚的答案,但我只是想分享我根据不同的人从不同类似的问题的答案所提出的解决scheme。 从Vojtech的回答上面得到了很大的帮助。

 extension UIView { func EZRoundCorners(corners:UIRectCorner, radius: CGFloat) -> CAShapeLayer { let path = UIBezierPath(roundedRect: self.bounds, byRoundingCorners: corners, cornerRadii: CGSize(width: radius, height: radius)) let mask = CAShapeLayer() mask.path = path.CGPath self.layer.mask = mask return mask } func EZRoundCornersWithBorder(corners:UIRectCorner, radius:CGFloat, color:UIColor, width:CGFloat) -> CAShapeLayer { let mask = self.EZRoundCorners(corners, radius: radius) // Add border let borderLayer = EZCALayer() borderLayer.path = mask.path // Reuse the Bezier path borderLayer.fillColor = UIColor.clearColor().CGColor borderLayer.strokeColor = color.CGColor borderLayer.lineWidth = width borderLayer.frame = self.bounds self.layer.addSublayer(borderLayer) return borderLayer } func removeEZLayers () { for layer in self.layer.sublayers! { if layer is EZCALayer { layer.removeFromSuperlayer() } } } } class EZCALayer : CAShapeLayer { } 

我从CAShapeLayerinheritance,所以我可以删除边框sublayers,如果我不想再使用它们。