简体   繁体   English

UIButton中的imageview属性和-(void)setImage:forState:有什么区别?

[英]What's the differences between imageview property and - (void)setImage:forState: in UIButton?

I just tried to set a new image (UIImage object) for my UIButton using myBtn.imageview.image = newimg; 我只是尝试使用myBtn.imageview.image = newimg;为我的UIButton设置新图像(UIImage对象) 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)" 之后,我得到使用setImage:forState:的正确解决方案,有关UIButton的文档说“ setImage:forState:是按钮的图像视图。(只读)”

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 the imageView . 如文档所述( "imageView is the button's image view. (read-only)" ),它是只读属性,您不能只更改imageView You have to use the recommended method setImage:forState: . 您必须使用推荐的方法setImage:forState: Basically UIButton might be having a different implementation than what you are expecting and myBtn.imageview.image may not actually set the desired image to the button. 基本上, UIButton可能与您期望的实现不同,并且myBtn.imageview.image可能实际上未将所需的图像设置为按钮。 setImage:forState: might be having an implementation which could be completely different from just setting myBtn.imageview.image = newimg; setImage:forState:可能具有与仅设置myBtn.imageview.image = newimg;完全不同的实现myBtn.imageview.image = newimg; . By directly setting like in your question, you might get some unexpected results since the actual implementation is not executed. 通过直接在问题中进行设置,您可能会得到一些意想不到的结果,因为未执行实际的实现。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM