UIImageView can't change position programmatically

为君一笑 提交于 2020-01-24 00:11:17

问题


I'm currently developing iPhone apps where i need to reposition the UIImageView. This is so far what i did so far:

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)

I have checked @IBAction is properly connected, but when I run the program, the UIImageView position does not changed. Can anyone help?


回答1:


In Storyboard, click on your UIImageView and add a constraint for the leading edge and a constraint for the top edge.

CTRL Drag from each constraint in the view hierarchy into your swift file and add IBOutlets for each constraint: Call them bubbleLeadingConstraint and bubbleTopConstraint.

Change you code to:

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

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

If you want to change the width/height, add constraints for that too and set in the code.

You can now change the position by changing the constraints. You can also animate any change by changing the constants and placing self.view.layoutIfNeeded() in an animation block.



来源:https://stackoverflow.com/questions/33101269/uiimageview-cant-change-position-programmatically

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!