Consider the following built in assertion:
button.isSelected = true
button.setImage(nil, for: .selected)
assert(button.image(for: .selected) === nil)
//^^^^^^^^^^^^^^ this trips with === and ==
Why? Am I making an invalid assumption?
Consider the following built in assertion:
button.isSelected = true
button.setImage(nil, for: .selected)
assert(button.image(for: .selected) === nil)
//^^^^^^^^^^^^^^ this trips with === and ==
Why? Am I making an invalid assumption?
From Apple's docs:
What may not be immediately clear is that calling:
can also be read as: "did not specify an image for
.selectedstate."So, the button will use the image from
.normal.If you want to remove the image when setting the button state to
.selected:Here is some sample code to play with:
Looks like this:
When running, toggling the
.isSelectedswitch will, as you might guess, setbutton.isSelectedtotrueorfalse.Toggling the button image state switches will either set the image to an image (if On) or set it to
nilif Off.Perhaps worth noting...
When a button has:
the normal properties (title, title color, image, etc) are used for the highlighted state.
This can be seen in the above example code by toggling
.isSelectedto On, and then tapping the button. We no longer see the "Highlighted" title or image.If that is your intended behavior, great! If not, you may need to re-think your approach.