I just tried to set a new image (UIImage object) for my UIButton using myBtn.imageview.image = newimg; then I found it didn’t work.
After that I get the correct solution that using setImage:forState: , the documentation about UIButton says “imageView is the button’s image view. (read-only)”
How should I get it? What’s the difference?
Thanks!
As the documentation says(
"imageView is the button’s image view. (read-only)"), it is a read only property and you cant just change theimageView. You have to use the recommended methodsetImage:forState:. BasicallyUIButtonmight be having a different implementation than what you are expecting andmyBtn.imageview.imagemay not actually set the desired image to the button.setImage:forState:might be having an implementation which could be completely different from just settingmyBtn.imageview.image = newimg;. By directly setting like in your question, you might get some unexpected results since the actual implementation is not executed.