ndarji
ndarji

Reputation: 39

Change button background image in swift

I am programming a dice app. I know how to change the image to different dice faces if I use UIImage view. But I want to use a UIButton for dice so that every time a user presses the dice button with a particular dice face image, the image updates to another dice image. I know how to use the arc4random to randomly change the image but I don't know how to apply that same concept on a UIButton.

Upvotes: 0

Views: 11218

Answers (3)

yassine menssi
yassine menssi

Reputation: 373

For swift 5

btn.setBackgroundImage(UIImage(named: "bg_btn.png"), for: .normal)

Upvotes: 0

Reefwing
Reefwing

Reputation: 2272

For Swift 4, the answer should be:

sender.setBackgroundImage(UIImage(named: "Button-Normal"), for: UIControl.State.normal)

Upvotes: 5

Kevin Morris
Kevin Morris

Reputation: 115

@IBAction func myImageButton(_ sender: UIButton) {
sender.setImage(UIImage(named: "Button-Normal"), for: UIControlState.normal)
}

Upvotes: 2

Related Questions