Snapkit: Animation Not working on remake

Created on 13 Aug 2018  路  3Comments  路  Source: SnapKit/SnapKit

Animation Not working on remake

Info | Value |
----------------------|-------------------------|
Platform | ios
Platform Version | 11.0
SnapKit Version | 4.0.0
Integration Method | cocoapods

I am going to change layout of label and apply remake constraints.
But animation does not working,

This code fire when user wants to start typing in textfield,

        UIView.animate(withDuration: AnimationInterval, animations: {


            self.lblName.font = UIFont(name: "Verdana", size: 12.0)

            self.lblName.snp.remakeConstraints({ (make) in

                make.centerX.equalTo(self)
                make.top.equalTo(self.margin)
                make.left.equalTo(self.margin)
                make.right.equalTo(-self.margin * 2)
                make.height.equalTo(self.lblHeight)
            })



        }) { (flag) in

            self.txtField.becomeFirstResponder()
        }

animation does not work.

Most helpful comment

        self.lblName.snp.remakeConstraints({ (make) -> Void in
            make.centerX.equalTo(self)
            make.top.equalTo(self.margin)
            make.left.equalTo(self.margin)
            make.right.equalTo(-self.margin * 2)
            make.height.equalTo(self.lblHeight)

        })

        UIView.animate(withDuration: AnimationInterval, animations: {

            self.layoutIfNeeded()


        }) { (flag) in

        }

this is done.

All 3 comments

take the remake constraints out of the animation block. make a func for animating, change the constraints, and then in the animation block, call the view's superview LayoutIfNeeded.

        self.lblName.snp.remakeConstraints({ (make) -> Void in
            make.centerX.equalTo(self)
            make.top.equalTo(self.margin)
            make.left.equalTo(self.margin)
            make.right.equalTo(-self.margin * 2)
            make.height.equalTo(self.lblHeight)

        })

        UIView.animate(withDuration: AnimationInterval, animations: {

            self.layoutIfNeeded()


        }) { (flag) in

        }

this is done.

@8bitramen's answer is correct, need to call layoutIfNeeded in the animation block and setNeedsLayout outside it after updating constraints

Was this page helpful?
0 / 5 - 0 ratings

Related issues

romk1n picture romk1n  路  3Comments

mkoppanen picture mkoppanen  路  3Comments

Cookiezby picture Cookiezby  路  8Comments

cjwirth picture cjwirth  路  4Comments

MoShenGuo picture MoShenGuo  路  4Comments