Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Swift: Change a button color once pressed

I'm trying to make a basic app my iPhone. I'm completely new to Xcode and I've had a browse on google and could not find exactly what I'm looking for. I'm trying to make a checklist for my plane.

I've basically got a View controller with several buttons on. And what I want is for the button to change from grey to blue once pressed and stay blue unless pressed again. I have no knowledge of Xcode and Swift language at all so if anyone could help please explain as if you're explaining to a kid.

So far I've managed to get the pages all setup and scrolling working but I just need the buttons to change now.

Thank you in advance, Ty.

like image 298
Ty Burgess Avatar asked Jul 12 '15 21:07

Ty Burgess


1 Answers

Use this method of UIButton class

func setTitleColor(_ color: UIColor?,
          forState state: UIControlState)

The state you are looking for is .Selected

And of course you have to handle selected state by yourself as a toggle button is not a native behavior.

like image 187
s1ddok Avatar answered Oct 02 '22 08:10

s1ddok