I’m a novice at Swift. I want to create a button that changes color when pressed by the user. However, it ought to revert back to the original color when the user removes the finger from the button.
I tried different methods shown in tutorials but to no avail.
2
Answers
import UIKit
class ViewController: UIViewController {
}
The modern approach to doing this is to supply a background color transformer function. Since your code already has a reference to the button (
firstTimeBtn
) and an implementation ofviewDidLoad
, a simple approach is to modify your code as follows:That gives you a button that’s red normally but is green while the user’s finger is tapping the button. Just change the colors to suit your desires. To set the button’s title in the same code, set its
configuration?.title
.