0

I am trying to add rounded corners to my views. I created a class:

class RoundedBorder: UIView {

override func awakeFromNib() {
    super.awakeFromNib()
    layer.cornerRadius = 10
}

}

I then applied it to my 3 UIViews but only the one in the middle, as you can see, was updated. They are in a stack view.

https://imgur.com/mEJR1uq

The rest of my app has custom classes to the various object elements, all working fine.

Any ideas?

Thanks

RajeshKumar R
  • 15,445
  • 2
  • 38
  • 70
  • Have you tried looking at this thread? https://stackoverflow.com/questions/1509547/giving-uiview-rounded-corners?rq=1 – Nicole Apr 10 '19 at 08:45
  • You have to use also `clipsToBounds = true` – Alastar Apr 10 '19 at 08:52
  • set `layer.masksToBounds = true`. All three views are rounded. But the image goes out of the bound so you cannot see it. The middle one has transparent BG so no problem with it. – RJE Apr 10 '19 at 10:21

5 Answers5

1

Try This !

These Are the Extensions you can use and make radius shadow every thing to buttons views tab bar even to navigation bar just put this code in your class end and on right side in inspector you can see different controllers which can help you :)

extension UIView {

    @IBInspectable
    var cornerRadius: CGFloat {
        get {
            return layer.cornerRadius
        }
        set {
            layer.cornerRadius = newValue
        }
    }

    @IBInspectable
    var borderWidth: CGFloat {
        get {
            return layer.borderWidth
        }
        set {
            layer.borderWidth = newValue
        }
    }

    @IBInspectable
    var borderColor: UIColor? {
        get {
            if let color = layer.borderColor {
                return UIColor(cgColor: color)
            }
            return nil
        }
        set {
            if let color = newValue {
                layer.borderColor = color.cgColor
            } else {
                layer.borderColor = nil
            }
        }
    }

    @IBInspectable
    var shadowRadius: CGFloat {
        get {
            return layer.shadowRadius
        }
        set {
            layer.shadowRadius = newValue
        }
    }

    @IBInspectable
    var shadowOpacity: Float {
        get {
            return layer.shadowOpacity
        }
        set {
            layer.shadowOpacity = newValue
        }
    }

    @IBInspectable
    var shadowOffset: CGSize {
        get {
            return layer.shadowOffset
        }
        set {
            layer.shadowOffset = newValue
        }
    }

    @IBInspectable
    var shadowColor: UIColor? {
        get {
            if let color = layer.shadowColor {
                return UIColor(cgColor: color)
            }
            return nil
        }
        set {
            if let color = newValue {
                layer.shadowColor = color.cgColor
            } else {
                layer.shadowColor = nil
            }
        }
    }
}


@IBDesignable extension UIButton {

    @IBInspectable var borderWidth: CGFloat {
        set {
            layer.borderWidth = newValue
        }
        get {
            return layer.borderWidth
        }
    }

    @IBInspectable var cornerRadius: CGFloat {
        set {
            layer.cornerRadius = newValue
        }
        get {
            return layer.cornerRadius
        }
    }

    @IBInspectable var borderColor: UIColor? {
        set {
            guard let uiColor = newValue else { return }
            layer.borderColor = uiColor.cgColor
        }
        get {
            guard let color = layer.borderColor else { return nil }
            return UIColor(cgColor: color)
        }
    }
}
Ali Xhah
  • 118
  • 7
0

Just try this extension

extension UIView {
    func roundCorners(by radius: CGFloat) {
        self.layer.cornerRadius = radius
        self.layer.masksToBounds = true
    }
}

And use it:

self.roundCorners(by: 10)
0

First: make sure clipsToBounds is set to true. Second: awakeFromNib() is not always called. There are many ways how a view gets instantiated. For instance, using storyboards would it be init?(coder aDecoder: NSCoder)

How about creating a Baseclass like this that covers all ways:

class YourView: UIView {    
init() {
    super.init(frame: .zero)
    setup()
}

required init?(coder aDecoder: NSCoder) {
    super.init(coder: aDecoder)
    setup()
}

override init(frame: CGRect) {
    super.init(frame: frame)
    setup()
}

func setup() {
    // do your stuff
} 
}
Helge Becker
  • 3,219
  • 1
  • 20
  • 33
0

Try This:-

extension UIView {

    @IBInspectable
    var cornerRadius: CGFloat {
    get {
        return layer.cornerRadius
    }
    set {
         layer.cornerRadius = newValue
      }
   }
}

put this code in AppDeleagte.

and go to storyboard clicked your label or view to set cornerRadius, and then you goto property and set radius you want.

enter image description here

and check the clipToBound = true.

enter image description here

Sagar Sangani
  • 287
  • 2
  • 7
0
 - self.view.layoutIfNeeded()

Use this code line before setting the corner radius of views, Also put it after setting radius if this doesn't work.

AtulParmar
  • 4,358
  • 1
  • 24
  • 45
Zeeshan Ahmed
  • 834
  • 8
  • 13