Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the best way to reuse the attributes from a UIButton in Swift

I have a bunch of buttons that I'm adding to a UIToolbar when a UITextField is tapped using inputAccessoryView. All of these buttons are identical except for the title and what I would like to be able to do is reuse the UI attributes such as titleColor, frame etc.

What would be the most efficient way to accomplish what I described above?

Here is the code...

    // code for first button
    let button1 = UIButton();
    button1.backgroundColor = UIColor.orangeColor()
    button1.setTitle("button1", forState: .Normal)
    button1.setTitleColor(UIColor.whiteColor(), forState: .Normal)
    button1.setTitleColor(UIColor.orangeColor(), forState: .Highlighted)
    button1.frame = CGRect(x:0, y:0, width:35, height:35)
    button1.addTarget(self, action: #selector(myFunction), forControlEvents: UIControlEvents.TouchUpInside)

    // code for second button which is identical to the first button
    let button2 = UIButton();
    button2.backgroundColor = UIColor.orangeColor()
    button2.setTitle("button2", forState: .Normal)
    button2.setTitleColor(UIColor.whiteColor(), forState: .Normal)
    button2.setTitleColor(UIColor.orangeColor(), forState: .Highlighted)
    button2.frame = CGRect(x:0, y:0, width:35, height:35)
    button2.addTarget(self, action: #selector(myFunction), forControlEvents: UIControlEvents.TouchUpInside)

    let barButton = UIBarButtonItem()
    barButton.customView = button

    let barButton2 = UIBarButtonItem()
    barButton2.customView = button2

    let toolBar = UIToolbar()
    toolBar.items = [ barButton, barButton2]
    toolBar.sizeToFit()

    myTextField.inputAccessoryView = toolBar
like image 730
fs_tigre Avatar asked Dec 28 '25 00:12

fs_tigre


2 Answers

Refactor the duplicate code into a single local function.

func configure(_ button:UIButton) {
    button.backgroundColor = UIColor.orangeColor()
    button.setTitleColor(UIColor.whiteColor(), forState: .Normal)
    button.setTitleColor(UIColor.orangeColor(), forState: .Highlighted)
    button.frame = CGRect(x:0, y:0, width:35, height:35)
    button.addTarget(self, action: #selector(myFunction), forControlEvents: UIControlEvents.TouchUpInside)
}

// code for first button
let button1 = UIButton()
button1.setTitle("button1", forState: .Normal)
configure(button1)

// code for second button which is identical to the first button
let button2 = UIButton()
button2.setTitle("button2", forState: .Normal)
configure(button2)
like image 51
matt Avatar answered Dec 30 '25 16:12

matt


Something like @matt's answer, but IMHO better:

// code for first button
let button1 = UIButton().configure("button1", frame: CGRect(x: 0, y: 0, width: 35, height: 35), target: self, action: #selector(myFunction))

// code for second button which is probably not exactly identical to the first button. They should at minimum have different titles, different frames and activate different functions.
let button2 = UIButton().configure("button2", frame: CGRect(x: 0, y: 0, width: 35, height: 35), target: self, action: #selector(myFunction))

Elsewhere in your code because you probably will use it in several view controllers:

extension UIButton {
    func configure(title: String, frame: CGRect, target: AnyObject, action: Selector) -> UIButton {
        self.backgroundColor = UIColor.orangeColor()
        self.setTitle(title, forState: .Normal)
        self.setTitleColor(UIColor.whiteColor(), forState: .Normal)
        self.setTitleColor(UIColor.orangeColor(), forState: .Highlighted)
        self.frame = frame
        self.addTarget(target, action: action, forControlEvents: UIControlEvents.TouchUpInside)
        return self
    }
}
like image 33
Daniel T. Avatar answered Dec 30 '25 17:12

Daniel T.



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!