I have a button that should change the image when pressed, but for some reason it does not change to the image set to 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) }
You must add an image extension.And there is no need to create separate image variables.
testBtn.setImage(UIImage(named:"a1.png"),forState:UIControlState.Normal) testBtn.setImage(UIImage(named:"a2.png"),forState:UIControlState.Highlighted)
I hope the problem may be with the image name, you may also need to provide an extension: Instead
let versusButtonClickedImage = UIImage(named: "versus_button_cicked") as UIImage
Using
let versusButtonClickedImage = UIImage(named: "versus_button_cicked.png") as UIImage
( , , 2014 ) , UIImage(named:), ., Obj-C .
UIImage(named:)
:
self.CButton.setImage(UIImage(named: "ArrowUp30.png"), forState: UIControlState.Normal)
self.CButton.setImage(UIImage(named: "ArrowUp30"), forState: UIControlState.Normal)
The state " highlighted" means pressing the finger on the button and keep pressing (without leaving the screen). Therefore, if the user "knocked" (press and leave) the button. It will recover to its “normal" state. You must set different images manually in the button action method.
highlighted
func buttonTapped() { let normalImage = self.button.imageForState(.Normal) let highlightedImage = self.button.imageForState(.Highlighted) button.setBackgroundImage(highlightedImage, forState: .Normal) button.setBackgroundImage(normalImage, forState: .Highlighted) }