What is the difference between UIbutton.setImage
and changing UIbutton.imageView
?
buttonA.imageView?.image = UIImage(named: "name")
buttonA.setImage(UIImage(named: "name"), for: .normal)
When I try to use setImage
and then try to get it’s position using frame.origin.x
, the position returned are not what I expected and so I used .imageView?.image
approach. But when I use that, I observed that clicking on the button changes the image for a brief time before continuing.
2
Answers
For some clarification…
If you set the
.image
property directly:You have not informed the button class that it has a new image property.
The same thing applies to setting the title:
That will change the text of the title label, but only until the next UI update… at which point UIKit will use the value assigned via:
That’s why it’s important to use both
.setImage(...)
and.setTitle(...)
instead of setting the property itself.