Peter Tretyakov Peter Tretyakov - 1 year ago 54
iOS Question

draw func doesn't work for button from storyboard

In my app I have several

UIButton
s, which I want to be with rounded corners. All of these buttons are placed in Storyboard.

I've created custom class
RoundedButton
, which has simple override of
draw
func.

import UIKit

class RoundedButton: UIButton {

override func draw(_ rect: CGRect) {
self.layer.cornerRadius = 4.0
}

}


I've added this custom class to every button in Storyboard, but buttons corners are not rounded in simulator.

Thanks for any help!

Answer Source

You should instead override init

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

   layer.cornerRadius = 4.0
}

EDITED:

if you don't use storyboard, or xib's

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

   layer.cornerRadius = 4.0
}