【问题标题】:Set of UIImageView.Image re-positions parent view一组 UIImageView.Image 重新定位父视图
【发布时间】:2015-11-13 13:21:53
【问题描述】:

我有一个 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

【问题讨论】:

    标签: swift uiview uiimageview


    【解决方案1】:

    你确定吗?在调用updateImage 后,您是否通过记录popup.frame.origin 进行检查?您是否为number uiimageview 正确设置了clipsToBoundscontentMode 属性?

    【讨论】:

    • 有趣的是,popup.frame.origin 总是相同的,但是 UIView 会在视觉上移动我可以在模拟器中看到...... self.popup.center 似乎有问题
    • 您是否尝试在viewDidLoad 中设置number.clipsToBouds = true
    • 刚刚试了一下。它没有帮助。我还在函数中添加了打印:DidLoad: (113.0, 43.0) - openPopUp: (95.5, 333.0) - updatePosition: (95.5, 333.0)
    • 当你有一个 UIImageView 时,你必须告知你想如何在其中渲染你的图像。为此,您必须设置 UIImageView 的 contentMode 属性。尝试在viewDidLoad 中设置number.contentMode = .ScaleToFit。在这种情况下,您的图像将适合您的 UIImageView。如果你的图片比你的 UIImageView 大并且你没有设置这个属性,你的图片会出现在边界之外(除非你设置了 number.clipsToBounds = true )。请阅读the apple doc了解更多详情。
    • 是的...我在情节提要中将其设置为直接在检查器中居中。我也在 viewdidload 中又做了一次。没有变化:-(我觉得这是一个错误
    【解决方案2】:

    我发现如果我在“文件检查器”选项卡的“界面生成器文档”部分中停用“使用自动布局”,则上述代码有效。

    此外,这里是使用自动布局和约束的问题的有效解决方案:

    Auto Layout center UIView above another UIView at its center

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2014-03-29
      • 1970-01-01
      • 2023-03-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多