Byoth
Byoth

Reputation: 1965

Image Not Removing For Disable state of UIButton

I made a button that is able to toggle the own isEnabled, and It's updating the title and image of button according to this state.

myButton.setTitle("Enabled Title", for: .normal)
myButton.setImage(UIImage(named: "enabled_resource_name"), for: .normal)

myButton.setTitle("Disabled Title", for: .disabled)
myButton.setImage(nil, for: .disabled)

The isEnabled of my button has toggled well. and title has also changed according to that. But I found a strange problem about changing image.

In the enabled to disabled case, the image UIImage(named: "enabled_resource_name") doesn't removed.

But It has changed a little. the image has become a little transparent when It's disabled. and in the disabled to enabled case, It does work fine.

Why does this happens?

Upvotes: 7

Views: 1648

Answers (1)

Taras Chernyshenko
Taras Chernyshenko

Reputation: 2829

Try to set UIImage() instead of nil, like:

myButton.setImage(UIImage(), for: .disabled)

Upvotes: 17

Related Questions