3

When I try to change button's image when button pressed but it just shows a blue square as button's image

@IBAction func button(_ sender: Any) {
     buttonOutlet.setImage(#imageLiteral(resourceName: "thing"), for: UIControl.State.normal)
}
TheNeil
  • 3,321
  • 2
  • 27
  • 52
Eren Özkan
  • 59
  • 1
  • 4

2 Answers2

4

From Apple documentation:

A control becomes highlighted when a touch event enters the control’s bounds...

Highlighted state of a control

button.setImage(UIImage(named: "button_normal_state"), for: .normal)
button.setImage(UIImage(named: "button_pressed_state"), for: .highlighted)
Idan Moshe
  • 1,675
  • 4
  • 28
  • 65
  • 1
    this one was the first method that I tried and I tried it again and it worked I don't know how or why it failed at the first time but thanks guys – Eren Özkan Apr 28 '19 at 11:08
2

Just recheck that you have "thing" - image in your target.

button.setImage(UIImage(named: "thing"), for: [.selected, .highlighted])

And see this question UIButton: set image for selected-highlighted state

Eysner
  • 584
  • 4
  • 15