ios – UIView的圆顶角,并添加边框

前端之家收集整理的这篇文章主要介绍了ios – UIView的圆顶角,并添加边框前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我有一个类别中的以下代码进行四舍五入.我也想画边界.但边角没有显示在角落的圆角上.

这是代码

  1. - (void) roundTopCorners:(CGFloat) radius
  2. {
  3. self.layer.masksToBounds = YES;
  4.  
  5. CGRect bounds = self.bounds;
  6. UIBezierPath *maskPath = [UIBezierPath bezierPathWithRoundedRect:bounds byRoundingCorners:(UIRectCornerTopLeft | UIRectCornerTopRight) cornerRadii:CGSizeMake(radius,radius)];
  7.  
  8. CAShapeLayer *maskLayer = [CAShapeLayer layer];
  9. maskLayer.frame = bounds;
  10. maskLayer.path = maskPath.CGPath;
  11. maskLayer.strokeColor = [UIColor redColor].CGColor;
  12.  
  13. self.layer.mask = maskLayer;
  14. }

解决方法

掩模层不被绘制,仅用于计算掩模.尝试:
  1. -(void)roundCorners:(UIRectCorner)corners radius:(CGFloat)radius
  2. {
  3. CGRect bounds = self.bounds;
  4. UIBezierPath *maskPath = [UIBezierPath bezierPathWithRoundedRect:bounds
  5. byRoundingCorners:corners
  6. cornerRadii:CGSizeMake(radius,radius)];
  7.  
  8. CAShapeLayer *maskLayer = [CAShapeLayer layer];
  9. maskLayer.frame = bounds;
  10. maskLayer.path = maskPath.CGPath;
  11.  
  12. self.layer.mask = maskLayer;
  13.  
  14. CAShapeLayer* frameLayer = [CAShapeLayer layer];
  15. frameLayer.frame = bounds;
  16. frameLayer.path = maskPath.CGPath;
  17. frameLayer.strokeColor = [UIColor redColor].CGColor;
  18. frameLayer.fillColor = nil;
  19.  
  20. [self.layer addSublayer:frameLayer];
  21. }
  22.  
  23. -(void)roundTopCornersRadius:(CGFloat)radius
  24. {
  25. [self roundCorners:(UIRectCornerTopLeft|UIRectCornerTopRight) radius:radius];
  26. }
  27.  
  28. -(void)roundBottomCornersRadius:(CGFloat)radius
  29. {
  30. [self roundCorners:(UIRectCornerBottomLeft|UIRectCornerBottomRight) radius:radius];
  31. }

您当前看到的框架是UITextField的正常框架,因此将框架样式设置为none.您还必须调整插图来弥补将框架样式设置为无,通常没有插入的事实.

猜你在找的iOS相关文章