mres
mres

Reputation: 143

How do I move an UI Object in swift that has its constraints set in a storyboard?

So I am trying to create a simple moving animation for my UIImageView. Problem is I've had its constraints set in the storyview (3 constraints, top, left and right positions). So I cannot just alter coordinates - my image doesn't move if I do so. So I guess I have to alter my constraints first. But I cannot even get them in code.

println(myPicture.constraints())

This returns an empty array. How do I animate an object that has its constraints set in the storyboard?

Upvotes: 2

Views: 2556

Answers (1)

Mike Cole
Mike Cole

Reputation: 1301

You can change the constant values of the constraints. I would make outlets for them. Something like this:

@IBOutlet var topConstraint: NSLayoutConstraint!

You can connect these in Storyboard just like any other outlet. It's easiest if you find the constraint in the left-side view hierarchy menu and drag there to make the connections. Then in your code:

topConstraint.constant = 50 // or whatever you want to set it to

That should do it. If you want to animate the movement, you could put it an an animation block. If the change isn't happening when you do this, try calling this afterwards:

UIView.animateWithDuration(0.2,
        animations: { () -> Void in
            self.topConstraint.constant = 50
            self.topConstraint.layoutIfNeeded()
        },
        completion: nil)

Upvotes: 1

Related Questions