UIButton is one of those classes where you have to take quite a few steps to set up an instance if you require anything beyond the default. For example, I would like to create a round button with a title, border, background colour, etc. extension UIButton { convenience init(withRadius radius:CGFloat, title:String, titleColor:UIColor, target:UIViewController, selector:Selector, backgroundColor:UIColor, borderColor:UIColor, borderWidth:CGFloat, position:CGPoint) { self.init() self.setTitle(title, for: .normal) self.setTitleColor(titleColor, for: .normal) self.addTarget(target, action: selector, for: .touchDown) self.backgroundColor = backgroundColor let diameter = 2 * radius self.frame = CGRect(x: position.x, y: position.y, width: diameter, height: diameter) self.layer.cornerRadius = radius self.layer.masksToBounds = false self.layer.borderWidth = borderWidth self.layer.borderColor = borderColor
Technology tips for writers, editors, designers, illustrators, programmers and publishers, and now Retro Gamers.