iOS 如何去除细边框线然后使用 layer.cornerRadius

iOS how to remove thin border line then use layer.cornerRadius

我总是使用简单的方法来获得视图圆角

+ (void)setRoundedCornersByView:(UIView*) givenView roundAngle:(int)roundAngle borderWidth:(double)borderWidth borderColor:(UIColor*)borderColor alphaBorder:(double)alphaBorder {
    givenView.layer.cornerRadius = roundAngle;
    givenView.layer.borderColor = [[borderColor colorWithAlphaComponent:alphaBorder] CGColor];
    givenView.layer.borderWidth = borderWidth;
    givenView.layer.masksToBounds = YES;
}

但是现在我在圆线周围有细边框,它是一条细线,颜色类似于圆角视图的背景颜色

如何在不使用 onDraw 的情况下将其删除,因为这是不可能的 - 因为这意味着我必须在需要圆角的地方覆盖所有 iOS 控件。

你也试试用

UIBezierPath *maskPath = [UIBezierPath bezierPathWithRoundedRect:bound byRoundingCorners:corners cornerRadii:cornerRadii];
CAShapeLayer *maskLayer = [[CAShapeLayer alloc] init];
maskLayer.frame = givenView.bounds;
maskLayer.path = maskPath.CGPath;
givenView.layer.mask = maskLayer;

但是,如您所见,它不是完全圆润的

很明显,图层边界不够大,无法覆盖角绘制剪辑后的整个图像。因此,您可以稍微扩大图层的边界以覆盖视图的图像。像这样:

+ (void)setRoundedCornersByView:(UIView *)givenView roundAngle:(int)roundAngle borderWidth:(double)borderWidth borderColor:(UIColor *)borderColor alphaBorder:(double)alphaBorder
{
  CGFloat offset = 1.f; // .5f is also good enough
  givenView.layer.cornerRadius = roundAngle + offset;
  givenView.layer.borderColor = [[borderColor colorWithAlphaComponent:alphaBorder] CGColor];
  givenView.layer.borderWidth = borderWidth + offset;
  givenView.layer.masksToBounds = YES;

  [givenView.layer setBounds:CGRectMake(-offset,
                                        -offset,
                                        CGRectGetWidth(givenView.frame)  + offset * 2.f,
                                        CGRectGetHeight(givenView.frame) + offset * 2.f)];
}