简体   繁体   English

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

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

I always use simple method to get view rounded corners 我总是使用简单的方法来获取圆角视图

+ (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;
}

But now i got thin border around rounded line, it's a thin line which have color like background color of rounded view 但是现在我在圆角线周围有细边框,这是一条细线,其颜色类似于圆角视图的背景色

在此处输入图片说明

How to remove it without use onDraw, because it's not possible to do - because it's mean i must override all iOS controls where i need rounded corners. 如何在不使用onDraw的情况下将其删除,因为这是不可能的-因为这意味着我必须覆盖所有需要圆角的iOS控件。

Also u try use 你也尝试使用

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;

but, as u can see, it's not fully rounded 但是,如您所见,它还没有完全取整

在此处输入图片说明

Obviously, the layer bounds is not big enough to cover the whole image after the clip of corner drawing. 显然,在绘制角点剪辑之后,图层边界不足以覆盖整个图像。 So, you can enlarge the layer's bounds a little bit to cover the view's image. 因此,您可以稍微扩大图层的边界以覆盖视图的图像。 Like this: 像这样:

+ (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