139

I am trying to change the image of a UIButton using Swift... What should I do

This is OBJ-C code.but I don't know with Swift:

[playButton setImage:[UIImage imageNamed:@"play.png"] forState:UIControlStateNormal];
Naresh
  • 16,698
  • 6
  • 112
  • 113
Lop Hu
  • 1,443
  • 2
  • 9
  • 8

12 Answers12

376

From your Obc-C code I think you want to set an Image for button so try this way:

let playButton  = UIButton(type: .Custom)
if let image = UIImage(named: "play.png") {
    playButton.setImage(image, forState: .Normal)
}

In Short:

playButton.setImage(UIImage(named: "play.png"), forState: UIControlState.Normal)

For Swift 3:

let playButton  = UIButton(type: .custom)
playButton.setImage(UIImage(named: "play.png"), for: .normal)
Leo Dabus
  • 229,809
  • 59
  • 489
  • 571
Dharmesh Kheni
  • 71,228
  • 33
  • 160
  • 165
17

Swift 5

yourButton.setImage(UIImage(named: "BUTTON_FILENAME.png"), for: .normal)
Farkas Antal
  • 310
  • 3
  • 9
15

in Swift 4, (Xcode 9) example to turn picture of button to On or Off (btnRec):

var bRec:Bool = true

@IBOutlet weak var btnRec: UIButton!
@IBAction func btnRec(_ sender: Any) {
    bRec = !bRec
    if bRec {
        btnRec.setImage(UIImage(named: "MicOn.png"), for: .normal)
    } else {
        btnRec.setImage(UIImage(named: "MicOff.png"), for: .normal)
    }
}
Gulfam Khan
  • 1,010
  • 12
  • 23
L.N.Vu
  • 369
  • 4
  • 8
10

assume that this is your connected UIButton Name like

@IBOutlet var btn_refresh: UIButton!

your can directly place your image in three modes

 // for normal state
btn_refresh.setImage(UIImage(named: "xxx.png"), forState: UIControlState.Normal)
     // for Highlighted state
      btn_refresh.setImage(UIImage(named: "yyy.png"), forState: UIControlState.Highlighted)

        // for Selected state
         btn_refresh.setImage(UIImage(named: "zzzz.png"), forState: UIControlState.Selected)

on your button action

  //MARK: button_refresh action
@IBAction func button_refresh_touchup_inside(sender: UIButton)
{
    //if you set the image on same  UIButton
    sender.setImage(UIImage(named: "newimage.png"), forState: UIControlState.Normal)

    //if you set the image on another  UIButton
       youranotherbuttonName.setImage(UIImage(named: "newimage.png"), forState: UIControlState.Normal)
}
Anbu.Karthik
  • 82,064
  • 23
  • 174
  • 143
  • You don't need to set the button as an outlet if you have an action for the button. The button parameter sender is a reference to the button so you can just do "let yourButton = sender as! UIButton" and then do the changes to the button as needed. – Micah Montoya Nov 09 '16 at 15:05
10

For anyone using Assets.xcassets and Swift 3, it'd be like this (no need for .png)

let buttonDone  = UIButton(type: .Custom)

if let image = UIImage(named: "Done") {
    self.buttonDone.setImage(image, for: .normal)
}
DS.
  • 2,846
  • 4
  • 30
  • 35
7

As of swift 3.0 .normal state has been removed.you can use following to apply normal state.

myButton.setTitle("myTitle", for: [])
PatientC
  • 273
  • 4
  • 9
7

Swift 5 and ensures the image scales to the size of the button but stays within the buttons bounds.

yourButton.clipsToBounds = true
yourButton.contentMode = .scaleAspectFill

// Use setBackgroundImage or setImage
yourButton.setBackgroundImage(UIImage(named: "yourImage"), for: .normal)
Alex Marchant
  • 570
  • 6
  • 21
5

I prefer the method of initializing my variables at the top first:

let playButton:UIButton!
var image:UIImage!

and then setting them in viewDidLoad

override func viewDidLoad {
  ...
  playButton = UIButton(type: .Custom)
  imagePlay = UIImage(named:"play.png")
  playButton.setImage(imagePlay, forState: .Normal)
}
Kristian
  • 21,204
  • 19
  • 101
  • 176
5

Yes, even we can change image of UIButton, by using flag.

class ViewController: UIViewController
{
    @IBOutlet var btnImage: UIButton!
    var flag = false

    override func viewDidLoad()
    {
        super.viewDidLoad()

        //setting default image for button
        setButtonImage()

    }

    @IBAction func btnClick(_ sender: Any)
    {
        flag = !flag
        setButtonImage()
    }

    func setButtonImage(){
        let imgName = flag ? "share" : "image"
        let image1 = UIImage(named: "\(imgName).png")!
        self.btnImage.setImage(image1, for: .normal)
    }

}

Here, after every click your button image will change alternatively.

1

you can actually do this by highlighting the button and within the insepctor on the right hand tool bar you can update the image. obviously you can do it in code also as stated previously but this is another option for you

Richy Garrincha
  • 277
  • 5
  • 17
  • This doesn't actually work. Where do you update the image? If you're talking about the different states like highlight, selected, etc, it doesn't work there, – KFDoom Jan 09 '20 at 08:25
1

In Swift 4.2 and Xcode 10.1

Add image for selected UIButton

button.addTarget(self, action: #selector(self.onclickDateCheckMark), for: .touchUpInside)//Target 
button.setImage(UIImage.init(named: "uncheck"), for: UIControl.State.normal)//When selected
button.setImage(UIImage.init(named: "check"), for: UIControl.State.highlighted)//When highlighted
button.setImage(UIImage.init(named: "check"), for: UIControl.State.selected)//When selected

But if you want to change selected button image you need to change it's selected state. Then only selected image will appear in your button.

@objc func onclickDateCheckMark(sender:UIButton) {
    if sender.isSelected == true {
        sender.isSelected = false
        print("not Selected")
    }else {
        sender.isSelected = true
        print("Selected")

    }
}
Naresh
  • 16,698
  • 6
  • 112
  • 113
0

in swift 3.0:

@IBOutlet weak var selectionButton: UIButton!

selectionButton.setImage(UIImage.addBlueIcon, for: .selected)
Marcelo Gracietti
  • 3,121
  • 1
  • 16
  • 24