UIImageView 无法以编程方式更改位置

UIImageView can't change position programmatically

我目前正在开发 iPhone 需要重新定位 UIImageView 的应用程序。这是到目前为止我所做的:

import UIKit
import CoreGraphics
import AVFoundation
import QuartzCore

class _DBubbleViewController: UIViewController {
    @IBOutlet weak var BubbleBackground: UIImageView!
    @IBOutlet weak var Bubble: UIImageView!


override func viewWillAppear(animated: Bool) {
Bubble.frame = CGRect(x: 168, y: 220, width: Bubble.bounds.size.width,      height: Bubble.bounds.size.height)

我检查过@IBAction是否正确连接,但是当我运行程序时,UIImageView位置没有改变。谁能帮忙?

在情节提要中,单击您的 UIImageView 并为前缘添加一个约束,为顶边添加一个约束。

CTRL 从视图层次结构中的每个约束拖动到您的 swift 文件并为每个约束添加 IBOutlets:将它们命名为 bubbleLeadingConstraintbubbleTopConstraint.

将您的代码更改为:

override func viewWillAppear(animated: Bool) {
  super.viewWillAppear()

  bubbleLeftConstraint.constant = 168;
  bubbleTopConstraint.constant = 220;
}

如果你想改变 width/height,也添加约束并在代码中设置。

您现在可以通过更改约束来更改位置。您还可以通过更改常量并将 self.view.layoutIfNeeded() 放在动画块中来为任何更改设置动画。