How to set image of UIButton in Swift 3?

18,956

Solution 1

IBAction func clickedon(_ sender: UIButton) {

if(gamestate[sender.tag] == 0 && gameisactive == true){

    gamestate[sender.tag] = activeplayer

if (activeplayer == 1){
    print("working")
   sender.setImage(UIImage(named: "Cross.png")!, forState: UIControlState.Normal)
   sender.setImage(UIImage(named:"CrossSelected.png")!, forState: UIControlState.Selected)

    activeplayer = 2
}
else {
    print("working2")
    sender.setImage(UIImage(named: "Nought.png")!, forState: UIControlState.Normal)
    sender.setImage(UIImage(named:"NoughtSelected.png")!, forState: UIControlState.Selected)
    activeplayer = 1

} 
}

Solution 2

You can try this :

let image = UIImage(named: "imagename.png")
yourButton.setBackgroundImage(image, for: .normal)

I try using other setImage calls but not working. I found this setBackgroundImage to let the image appear. Hope that it will helps you.

Solution 3

In your code, UIControlState() can be replaced with UIControlState.normal or UIControlState.highlighted.

https://developer.apple.com/reference/uikit/uicontrolstate

IBAction func clickedon(_ sender: UIButton) {

    if(gamestate[sender.tag] == 0 && gameisactive == true){

        gamestate[sender.tag] = activeplayer

    if (activeplayer == 1){
        print("working")
        sender.setImage(UIImage(named:"Cross.png"), for: UIControlState.normal)
        sender.setImage(UIImage(named:"Cross_highlighted.png"), for: UIControlState.highlighted)
        activeplayer = 2
    }
    else {
        print("working2")
        sender.setImage(UIImage(named:"Nought.png"), for: UIControlState.normal)
        sender.setImage(UIImage(named:"Nought_highlighted.png"), for: UIControlState.highlighted)
        activeplayer = 1

    } 
}
Share:
18,956
Aditya Yadav
Author by

Aditya Yadav

Updated on June 04, 2022

Comments

  • Aditya Yadav
    Aditya Yadav almost 2 years

    Originally, my code was working.

    IBAction func clickedon(_ sender: UIButton) {
    
        if(gamestate[sender.tag] == 0 && gameisactive == true){
    
            gamestate[sender.tag] = activeplayer
    
        if (activeplayer == 1){
            print("working")
            sender.setImage(UIImage(named:"Cross.png"), for: UIControlState()) <-----------
            activeplayer = 2
        }
        else {
            print("working2")
            sender.setImage(UIImage(named:"Nought.png"), for: UIControlState()) <-----------
            activeplayer = 1
    
        } 
        }
    

    These two pieces of code that set image were working. These buttons have nothing set to their image. When they are clicked on, I want them to switch to those images. Those images are in the folder with the view controller. I am not sure why they stopped working. I commented out all other code to see if it could be an issue but it doesn't seem to be. The print statements get printed out in both cases. It's just the set image that doesn't seem to do anything. There is no error, it just does nothing.

    I don't really understand the UIControlState() code. I am mimicking an online project that has the same code and it works. It is really strange. Please let me know if you have any suggestions!