Uibutton At Your Convenience
UIButton is 1 of those classes where y'all receive got to receive got quite a few steps to ready an instance if y'all require anything beyond the default. For example, I would similar to practise a circular push clitoris alongside 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 allow diameter = ii * radius self.frame = CGRect(x: position.x, y: position.y, width: diameter, height: diameter) self.layer.cornerRadius = radius self.layer.masksToBounds = imitation self.layer.borderWidth = borderWidth self.layer.borderColor = borderColor.cgColor } }I'm avoiding difficult coding sizes, colours, etc. because I desire the code to endure flexible but calling this initializer prevents me from forgetting to laid whatever of the properties, therefore that instead of having to instantiate each push clitoris too therefore brand adjustments, nosotros tin terminate endure all done inwards 1 trouble of code:
let push clitoris = UIButton(withRadius: 50, title: "My Button", titleColor: .white, target: self, selector: #selector(buttonTapped(sender:)), backgroundColor: .gray, borderColor: .gray, borderWidth: 1.0, position: CGPoint.zero)We therefore demand to only add together the push clitoris to our view:
view.addSubview(button)And inwards this instance nosotros equally good demand the Selector that has been specified:
@objc func buttonTapped(sender:AnyObject) { allow but = sender as! UIButton if allow col = but.backgroundColor { switch col { instance .gray: UIView.animate(withDuration: 0.75, animations: { but.backgroundColor = .white but.setTitleColor(.gray, for: .normal) }) default: UIView.animate(withDuration: 0.75, animations: { but.backgroundColor = .gray but.setTitleColor(.white, for: .normal) }) }} }
Why the convenience initializer?
It is logical to purpose a convenience initializer because nosotros are instantiating the push clitoris from the initializer too therefore setting it up. You volition equally good discovery that if y'all endeavour to practise a designated initializer inside a UIButton extension y'all volition endure informed past times the compiler that this is non permitted.Convenience initializers are secondary, supporting initializers for a class. You tin terminate define a convenience initializer to telephone phone a designated initializer from the same cast equally the convenience initializer alongside roughly of the designated initializer’s parameters laid to default values. You tin terminate equally good define a convenience initializer to practise an instance of that cast for a specific purpose instance or input value type. (The Swift Programming Language, Apple)
Comments
Post a Comment