trying to animate a constraint in swift trying to animate a constraint in swift swift swift

trying to animate a constraint in swift


You need to first change the constraint and then animate the update.
This should be in the superview.

self.nameInputConstraint.constant = 8

Swift 2

UIView.animateWithDuration(0.5) {    self.view.layoutIfNeeded()}

Swift 3, 4, 5

UIView.animate(withDuration: 0.5) {    self.view.layoutIfNeeded()}


SWIFT 4 and above:

self.mConstraint.constant = 100.0UIView.animate(withDuration: 0.3) {        self.view.layoutIfNeeded()}

Example with completion:

self.mConstraint.constant = 100UIView.animate(withDuration: 0.3, animations: {        self.view.layoutIfNeeded()    }, completion: {res in        //Do something})


It's very important to point out that view.layoutIfNeeded() applies to the view subviews only.

Therefore to animate the view constraint, it is important to call it on the view-to-animate superview as follows:

    topConstraint.constant = heightShift    UIView.animate(withDuration: 0.3) {        // request layout on the *superview*        self.view.superview?.layoutIfNeeded()    }

An example for a simple layout as follows:

class MyClass {    /// Container view    let container = UIView()        /// View attached to container        let view = UIView()    /// Top constraint to animate    var topConstraint = NSLayoutConstraint()    /// Create the UI hierarchy and constraints    func createUI() {        container.addSubview(view)        // Create the top constraint        topConstraint = view.topAnchor.constraint(equalTo: container.topAnchor, constant: 0)        view.translatesAutoresizingMaskIntoConstraints = false        // Activate constaint(s)        NSLayoutConstraint.activate([           topConstraint,        ])    }    /// Update view constraint with animation    func updateConstraint(heightShift: CGFloat) {        topConstraint.constant = heightShift        UIView.animate(withDuration: 0.3) {            // request layout on the *superview*            self.view.superview?.layoutIfNeeded()        }    }}