Reputation: 41
I have a button which is supposed to change image when it is clicked but for some reason it is not changing to the image set for the UIControlState.Highlighted state
override func viewDidLoad() {
super.viewDidLoad()
let versusButtonClickedImage = UIImage(named: "versus_button_cicked") as UIImage
let versusButtonImage = UIImage(named: "versus_button") as UIImage
versusButton.setImage( versusButtonImage, forState: UIControlState.Normal)
versusButton.setImage(versusButtonClickedImage, forState: UIControlState.Highlighted)
}
Upvotes: 4
Views: 11042
Reputation: 802
The 'highlighted
' state means a finger press in the button and keep pressing (without leaving the screen). So if user 'tapped' (press then leave) the button. It would restore to the 'normal' state. You should set different images manually in the button's action method.
func buttonTapped() {
let normalImage = self.button.imageForState(.Normal)
let highlightedImage = self.button.imageForState(.Highlighted)
button.setBackgroundImage(highlightedImage, forState: .Normal)
button.setBackgroundImage(normalImage, forState: .Highlighted)
}
Upvotes: 0
Reputation: 19722
For the record, (at least as of today, December 2014), you need to provide the image name INCLUDING extension to UIImage(named:)
for it to work.
As far as I know, in Obj-C you don't have to.
Example:
self.CButton.setImage(UIImage(named: "ArrowUp30.png"), forState: UIControlState.Normal)
But this one does not work:
self.CButton.setImage(UIImage(named: "ArrowUp30"), forState: UIControlState.Normal)
Upvotes: 1
Reputation: 23892
You have to add extension of image.
And no need to create separate variables of image.
testBtn.setImage(UIImage(named:"a1.png"),forState:UIControlState.Normal)
testBtn.setImage(UIImage(named:"a2.png"),forState:UIControlState.Highlighted)
Upvotes: 9
Reputation: 15335
Hopefully, the problem might be with the image name, you might need to be provide with their extension as well : Instead of
let versusButtonClickedImage = UIImage(named: "versus_button_cicked") as UIImage
Use
let versusButtonClickedImage = UIImage(named: "versus_button_cicked.png") as UIImage
Upvotes: 1