3

I know this question already asked so many times and their solutions also available but nothing works for me. I am using Swift3 and I want to round any two sides of UIView for this purpose I found the following solution

Create a rectangle with just two rounded corners in swift?

and following is my code snippet of the above solution

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

}

This is the result I got. I don't know what I am doing wrong. Can anyone please solve this?

enter image description here

Community
  • 1
  • 1
Usman Javed
  • 2,437
  • 1
  • 17
  • 26

2 Answers2

28

Code tested and worked in Swift 3.

Use below extension to create 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
}
}

Usage: in viewDidLoad

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

Output:

enter image description here

Joe
  • 8,868
  • 8
  • 37
  • 59
  • This was the best short and Sweet :) – Raghav Chopra Jul 18 '17 at 10:37
  • 1
    Calling roundCorners() in viewDidLoad() applies a mask before the the view is laid out and doesn't account for any future layout changes. If you're going to call this from the ViewController, you should call it in viewDidLayoutSubviews(). Another option is create a UIView subclass with variables for the corners and radii that updates the mask in its layoutSubviews() – GingerBreadMane Apr 25 '19 at 19:37
-3

First of all you will need to Make IBOutlet for that Textfield or Button which you need to Be Rounded Corner

  1. Go to The ViewDidLoad() Method
  2. Write the name of IBOutLet
  3. Example Code mybutton.layer.cornerRadius=10
  4. Here is the Solution to Your Problem