The Dude The Dude - 1 year ago 75
Swift Question

UIControlState,Highlighted only respond from second click/touch

Ive got this line of code that changes the image on a button when clicked:

sender.setImage(UIImage(named: "something.png")!, forState: .Highlighted)

but it never works on the first touch. Can anyone explain to me what I'm doing wrong?

The default image is set in the storyboard.

Answer Source

When you call setImage(_:for:), that is setting the buttons image for the passed in state. You don't have to call it every time the button is tapped. Try call setImage(_:for:) in viewDidLoad, instead of in your IBAction.

The problem is the button has the default highlight state until its first tapped, which is why you are seeing what you are seeing.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download