swift - UIImageView.Image 集重新定位父 View

标签 swift uiview uiimageview

我有一个 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 的 clipsToBoundscontentMode 属性?

关于swift - UIImageView.Image 集重新定位父 View ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33693713/

相关文章:

iOS 捕获 UIView 里面有 UIImageView

swift - 如何将 UIImageView 更改为自定义单元格

ios - CATiledLayer 和 UIImageView 他们之间有什么大不了的?

ios - 与 CGContextDrawImage 相比,为什么 UIImageView 如此占用内存

ios - bringSubviewToFront 不适用于下拉 View (UIView)

swift - CABasicAnimation 链接不起作用

swift - 从字符串到 Nsdate Swift

ios - 使用 NSPredicate 选择任一输出

ios访问主窗口或 View

iphone - 关闭模态视图 Controller