2016-11-11 84 views
1

我知道這個問題已經問了很多次,他們的解決方案也可用,但沒有任何適用於我。 我使用Swift3,我想圓的UIView任何雙方爲了這個目的,我發現了以下解決方案Swift3 UIView的兩個角落

Create a rectangle with just two rounded corners in swift?

及以下爲上述方案

extension UIView { 

    /** 
    Rounds the given set of corners to the specified radius 

    - parameter corners: Corners to round 
    - parameter radius: Radius to round to 
    */ 
    func round(corners: UIRectCorner, radius: CGFloat) -> Void { 
     layer.addSublayer(_round(corners: corners, radius: radius)) 
    } 

    /** 
    Rounds the given set of corners to the specified radius with a border 

    - parameter corners:  Corners to round 
    - parameter radius:  Radius to round to 
    - parameter borderColor: The border color 
    - parameter borderWidth: The border width 
    */ 
    func round(corners: UIRectCorner, radius: CGFloat, borderColor: UIColor, borderWidth: CGFloat) { 
     let mask = _round(corners: corners, radius: radius) 
     addBorder(mask: mask, borderColor: borderColor, borderWidth: borderWidth) 
    } 

    /** 
    Fully rounds an autolayout view (e.g. one with no known frame) with the given diameter and border 

    - parameter diameter: The view's diameter 
    - parameter borderColor: The border color 
    - parameter borderWidth: The border width 
    */ 
    func fullyRound(diameter: CGFloat, borderColor: UIColor, borderWidth: CGFloat) { 
     layer.masksToBounds = true 
     layer.cornerRadius = diameter/2 
     layer.borderWidth = borderWidth 
     layer.borderColor = borderColor.cgColor; 
    } 

} 

private extension UIView { 

    func _round(corners: UIRectCorner, radius: CGFloat) -> CAShapeLayer { 
     let path = UIBezierPath(roundedRect: bounds, byRoundingCorners: corners, cornerRadii: CGSize(width: radius, height: radius)) 
     let mask = CAShapeLayer() 
     mask.path = path.cgPath 
     self.layer.mask = mask 
     return mask 
    } 

    func addBorder(mask: CAShapeLayer, borderColor: UIColor, borderWidth: CGFloat) { 
     let borderLayer = CAShapeLayer() 
     borderLayer.path = mask.path 
     borderLayer.fillColor = UIColor.clear.cgColor 
     borderLayer.strokeColor = borderColor.cgColor 
     borderLayer.lineWidth = borderWidth 
     borderLayer.frame = bounds 
     layer.addSublayer(borderLayer) 
    } 

} 

這是我的代碼片段是我得到的結果。我不知道我做錯了什麼。 任何人都可以解決這個問題嗎?

enter image description here

+0

向我們展示一些代碼 – Manishankar

回答

22

代碼測試和斯威夫特曾下文extension 3.

用於創建roundCorners

extension UIView { 
func roundCorners(corners:UIRectCorner, radius: CGFloat) { 
    let path = UIBezierPath(roundedRect: self.bounds, byRoundingCorners: corners, cornerRadii: CGSize(width: radius, height: radius)) 
    let mask = CAShapeLayer() 
    mask.path = path.cgPath 
    self.layer.mask = mask 
} 
} 

用法:viewDidLoad

yourViewName.roundCorners(corners: [.topRight, .bottomLeft], radius: 10) 

輸出:

enter image description here

+0

這是最好的短和甜蜜:) –

-1

所有你需要做IBOutlet中爲文本字段或按鈕,您 第一需要循環ED角

  1. 轉到viewDidLoad中()方法
  2. 收件IBOutlet中的名稱
  3. 示例代碼mybutton.layer.cornerRadius = 10
  4. 這裏是解決問題的方法
+0

他特別問你如何繞過兩個角落,你的答案設置所有角落的角落半徑。 – AlexCatch