Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Swift set button image

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)
}
like image 866
Eren Özkan Avatar asked Oct 15 '25 18:10

Eren Özkan


2 Answers

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)
like image 70
Idan Moshe Avatar answered Oct 17 '25 07:10

Idan Moshe


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

like image 22
Eysner Avatar answered Oct 17 '25 08:10

Eysner



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!