简体   繁体   中英

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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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