问题
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