ios - `UIbutton.setImage` 和更改 `UIbutton.imageView` 有什么区别?
问题描述
UIbutton.setImage
和 改变 和有什么不一样UIbutton.imageView
?
buttonA.imageView?.image = UIImage(named: "name")
buttonA.setImage(UIImage(named: "name"), for: .normal)
当我尝试使用setImage
然后尝试使用 获取它的位置frame.origin.x
时,返回的位置不是我所期望的,所以我使用了.imageView?.image
方法。但是当我使用它时,我观察到单击按钮会在短时间内更改图像,然后再继续。
解决方案
For some clarification...
If you set the .image
property directly:
buttonA.imageView?.image = UIImage(named: "name")
You have not informed the button class that it has a new image property.
The same thing applies to setting the title:
buttonA.titleLabel?.text = "My 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:
buttonA.setTitle("Button A", for: .normal)
That's why it's important to use both .setImage(...)
and .setTitle(...)
instead of setting the property itself.
推荐阅读
- javascript - CORS 错误:使用 res.redirect() 时没有“Access-Control-Allow-Origin”标头
- django - 在模板标签过滤器Django中请求cart_obj
- android - 如何修复此错误,当我尝试从 react-native 项目构建 .apk 时发生
- python - 为 df2 pandas 数据框中的每个行条目添加计算列到 df2
- javascript - 如何将 DOM 事件作为事件处理程序绑定方法传递
- swift - 如何在 Swift 中将两个 UIImage 组合成一个图像?
- c++ - 为什么在虚函数声明中使用不同的返回类型会引发错误而不是导致重新定义?
- typescript - 如何为打字稿中的所有类方法声明一种类型
- angular - 如何用角材料更新表格中的一行?
- python - 插入 PostgreSQL 时出现“TypeError:字符串格式化期间并非所有参数都转换”