繁体   English   中英

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)];
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM