2014-08-28 56 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) 
    } 

}