2014-08-28 52 views
6

我的应用大多是圆形和边框的基础。带边框的cornerRadius:边框的一些小故障

我使用UIButton,UIImageView和UIView的图层属性给出圆角半径和边框。

但我面临着不干净的角落问题。

我得到如下结果。

的UIButton

enter image description here

的UIImageView:

enter image description here

你可以观察周围的白色或灰色的边框窄边框线。

这是我的代码:

button.layer.borderWidth = 2.0; 
button.layer.borderColor = [[UIColor whiteColor] CGColor]; 
button.layer.cornerRadius = 4; 

button.clipsToBounds = YES; 

我必须寻找解决这个,但没有得到成功。我试过button.layer.masksToBounds = YES但没有效果。

我是否缺少任何东西?还是有其他方法可以给我更好的结果比较CALayer

如果您需要更多信息,请点评。谢谢你给你时间。

+1

是它出问题的时候borderWith是一个整数吗? – 2014-08-28 14:30:31

+0

为了测试我尝试了borderWidth 1.5,2.0,2.5和3。0但我总是有同样的毛病。 – CRDave 2014-08-28 14:47:09

回答

12

我试了很多的解决方案,并最终通过UIBezierPath

我创建了UIView的类别并添加了制作圆形矩形和边框的方法。

这是该类别的方法:

- (void)giveBorderWithCornerRadious:(CGFloat)radius borderColor:(UIColor *)borderColor andBorderWidth:(CGFloat)borderWidth 
{ 
    CGRect rect = self.bounds; 

    //Make round 
     // Create the path for to make circle 
     UIBezierPath *maskPath = [UIBezierPath bezierPathWithRoundedRect:rect 
                 byRoundingCorners:UIRectCornerAllCorners 
                  cornerRadii:CGSizeMake(radius, radius)]; 

     // Create the shape layer and set its path 
     CAShapeLayer *maskLayer = [CAShapeLayer layer]; 

     maskLayer.frame = rect; 
     maskLayer.path = maskPath.CGPath; 

     // Set the newly created shape layer as the mask for the view's layer 
     self.layer.mask = maskLayer; 

    //Give Border 
     //Create path for border 
     UIBezierPath *borderPath = [UIBezierPath bezierPathWithRoundedRect:rect 
                 byRoundingCorners:UIRectCornerAllCorners 
                   cornerRadii:CGSizeMake(radius, radius)]; 

     // Create the shape layer and set its path 
     CAShapeLayer *borderLayer = [CAShapeLayer layer]; 

     borderLayer.frame  = rect; 
     borderLayer.path  = borderPath.CGPath; 
     borderLayer.strokeColor = [UIColor whiteColor].CGColor; 
     borderLayer.fillColor = [UIColor clearColor].CGColor; 
     borderLayer.lineWidth = borderWidth; 

     //Add this layer to give border. 
     [[self layer] addSublayer:borderLayer]; 
} 

我得到使用UIBezierPath从这个惊人的文章的想法:Thinking like a Bézier path

我得到这两个连接的大部分代码:

备注:这是类别方法,所以self代表调用此方法的视图。像的UIButton,UIImageView的等

+0

这是非常好的解决方案。 当我使用'layer.cornerRadius'四舍五入时,我遇到了很薄的黑色轮廓的奇怪问题,我使用你发布的代码片段修复了它。谢谢! – 2015-12-06 14:02:16

+0

您忘记使用borderColor参数;) – 2016-06-23 06:45:14

+0

@ KamilNomtek.com borderLayer.strokeColor = [UIColor whiteColor] .CGColor;是边框颜色。 – CRDave 2016-06-23 06:56:13

-3

删除

button.layer.borderWidth = 0.3; 
button.layer.borderColor = [[UIColor blueMain] CGColor]; 
+0

但我需要边框。 – CRDave 2014-08-29 04:58:15

+0

那条黑色的细线不是我给的边框。查看编辑的代码。我给白色厚边框第二个图像和灰色边框到第一个。机器人我得到额外的思考边界,这是我的问题。 – CRDave 2014-08-29 04:59:10

5

这里作为UIView的的扩展,我@ CRDave的answer的斯威夫特版本:

protocol CornerRadius { 
    func makeBorderWithCornerRadius(radius: CGFloat, borderColor: UIColor, borderWidth: CGFloat) 
} 

extension UIView: CornerRadius { 

    func makeBorderWithCornerRadius(radius: CGFloat, borderColor: UIColor, borderWidth: CGFloat) { 
     let rect = self.bounds; 

     let maskPath = UIBezierPath(roundedRect: rect, byRoundingCorners: .AllCorners, cornerRadii: CGSize(width: radius, height: radius)) 

     // Create the shape layer and set its path 
     let maskLayer = CAShapeLayer() 
     maskLayer.frame = rect 
     maskLayer.path = maskPath.CGPath 

     // Set the newly created shape layer as the mask for the view's layer 
     self.layer.mask = maskLayer 

     //Create path for border 
     let borderPath = UIBezierPath(roundedRect: rect, byRoundingCorners: .AllCorners, cornerRadii: CGSize(width: radius, height: radius)) 

     // Create the shape layer and set its path 
     let borderLayer = CAShapeLayer() 

     borderLayer.frame  = rect 
     borderLayer.path  = borderPath.CGPath 
     borderLayer.strokeColor = borderColor.CGColor 
     borderLayer.fillColor = UIColor.clearColor().CGColor 
     borderLayer.lineWidth = borderWidth * UIScreen.mainScreen().scale 

     //Add this layer to give border. 
     self.layer.addSublayer(borderLayer) 
    } 

}