swift change button color on tap programmatically code example

Example 1: change opacity of button clicked swift

@IBAction func keyPressed(_ sender: UIButton) {
  // sets opacity to half - you can change the opacity by editing "0.5"
  sender.alpha = 0.5
  
  /* 
  Code should execute after 0.2 second delay.
  You can change delay by editing 0.2.
  */
  DispatchQueue.main.asyncAfter(deadline: .now() + 0.2) {
    // Bring's sender's opacity back up to fully opaque
    sender.alpha = 1.0
  }
  
 }

Example 2: set color for uibutton programmatically swift

button.setTitleColor(.red, for: .normal)

Tags:

Misc Example