3
ViewController: UIViewController {
   @IBAction func like(sender: AnyObject) {
       like(backgroundColor!) = UIColor.greenColor()
   }
}

I want to chanage the color of UIbutton"like button" : White is the default "not tapped or unlike" green when is tapped.

How do you change a buttons background colour when it is tapped using Swift?

nhgrif
  • 61,578
  • 25
  • 134
  • 173
JAMESMATHEO12
  • 55
  • 2
  • 4

1 Answers1

4

The button isn't like -- that's the method's name. You can access the button through sender.

@IBAction func like(sender: AnyObject) {
    (sender as UIButton).backgroundColor = UIColor.green
}

Or more concise:

@IBAction func like(button: UIButton ) {
    button.backgroundColor = UIColor.green
}
Jacopo Penzo
  • 2,168
  • 2
  • 24
  • 29
Lyndsey Scott
  • 37,080
  • 10
  • 92
  • 128