Usman Javed
Usman Javed

Reputation: 2455

Swift3 round two corners of UIView

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

Upvotes: 3

Views: 4014

Answers (2)

Muhammad Zeshan Mazhar
Muhammad Zeshan Mazhar

Reputation: 445

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

Upvotes: -3

Joe
Joe

Reputation: 8986

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

Upvotes: 28

Related Questions