我有一个 UIView 并添加了一个 UIImageView 作为子项。后来我通过另一个 UIButton 事件更改了 UIView 的位置。
如果我更改 UIImageView 的图像,则 UIView - inc.所有护盾 - 移回初始位置。
有人可以解释一下这里发生了什么吗:-D 我如何保持 UIView 的新位置?
Here a image screen I took to visualize the issue
import UIKit
class ViewController: UIViewController {
@IBOutlet weak var number: UIImageView!
@IBOutlet weak var popup: UIView!
@IBAction func openPopup(sender: UIButton) {
self.popup.center = sender.center
//old position: (190.5, 124.0) - new Position: (173.0, 414.0)
}
@IBAction func updateImage(sender: UIButton) {
self.number.image = UIImage(named: "img-1")
}
}
更新:
我发现如果我停用自动布局,上面的代码就可以工作。这是使用自动布局来执行此操作的解决方案: Auto Layout center UIView above another UIView at its center
最佳答案
你确定吗?您是否在调用 updateImage
后通过记录 popup.frame.origin
进行检查?您是否正确设置了 number
uiimageview 的 clipsToBounds
和 contentMode
属性?
关于swift - UIImageView.Image 集重新定位父 View ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33693713/