Amir_P
Amir_P

Reputation: 9019

Add Constraint with animation to view

hey I'm trying to bring inside a view with changing it's constraint with this code

    UIView.animate(withDuration: 500, animations: {
        self.forgetTopConstraint.isActive = false
        self.view.addConstraint(NSLayoutConstraint(item: self.view, attribute: NSLayoutAttribute.centerY, relatedBy: NSLayoutRelation.equal, toItem: self.forgetPasswordView, attribute: NSLayoutAttribute.centerY, multiplier: 1, constant: 0))
    })

forgetTopConstraint is a constraint that anchors forgetPasswordView's top to view's bottom with constant 0 so the forgetTopConstraint is out of view then I'm trying to animate it inside to the center of view with that code but it's appearing suddenly not with animation

Upvotes: 1

Views: 356

Answers (1)

Christopher Griffith
Christopher Griffith

Reputation: 93

Without seeing more of your surrounding code or view setup, it sounds like you just need to call layoutIfNeeded on the containing view. That's the method that actually performs the work of applying updated constraints. layoutIfNeeded is triggered automatically by a number of other changes to the view, but in the case of wanting it to animate, you actually don't need to call any of your other layout changes inside the animation block. Just call the one statement and that should cause it to animate all your previous changes:

self.forgetTopConstraint.isActive = false
self.view.addConstraint(NSLayoutConstraint(item: self.view, attribute: NSLayoutAttribute.centerY, relatedBy: NSLayoutRelation.equal, toItem: self.forgetPasswordView, attribute: NSLayoutAttribute.centerY, multiplier: 1, constant: 0))
UIView.animate(withDuration: 500, animations: {
    self.view.layoutIfNeeded()    
})

Upvotes: 1

Related Questions