0

I am creating a custom button which (almost) works fine. It is supposed to change it's image when the button is pressed down, and the change back again when you let loose. The problem is that it takes one click before it starts working.

@IBOutlet weak var myButton: UIButton!

@IBAction func myButton(sender: UIButton)  {

        myButton.setImage(UIImage(named: "myImage.png")!, forState: .Normal)
        myButton.setImage(UIImage(named: "myImagePressed.png")!, forState: .Highlighted)
        myButton.setImage(UIImage(named: "myImagePressed.png")!, forState: .Selected)

    }
Peter Hornsby
  • 4,208
  • 1
  • 25
  • 44
Nils
  • 313
  • 1
  • 2
  • 14
  • 1
    You have to put that `setImage...`-code in `viewDidLoad` or simply not use code at all but the InterfaceBuilder instead. – luk2302 Jan 19 '16 at 12:05

1 Answers1

0

Since you've put your code to the IBAction, it only gets executed once the button is pressed. It is also unecessarily called every time the button is pressed. You need to run that code before the button is pressed. The best place is probably viewDidLoad() of the viewcontroller that controls the view the button is part of.

Mirek
  • 470
  • 4
  • 18