Master Coder
Master Coder

Reputation: 48

Animate constraint change not working

I have a view and I want to move it according to its top spacing constraint inside the animation block.

This is the code I use. The constraint is changing, but the frame is still the same; what should I do?

I have an IBOutlet to the constraint named: topConstraint

  [UIView animateWithDuration:0.3f animations:^{
        self.topConstraint.constant -= 80 ;
        [self.view layoutIfNeeded];
    }];

I have read everything about this, but it doesn't seem to work.

Upvotes: 2

Views: 2218

Answers (3)

pableiros
pableiros

Reputation: 16062

This is what it worked for me in Swift 3

self.view.setNeedsUpdateConstraints()

UIView.animate(withDuration: 1.0,
                       animations: {
                        self.horizontalCenterConstraint.constant = -23
                        self.view.layoutIfNeeded()
        }, completion: nil)

Upvotes: 1

F1ank3r
F1ank3r

Reputation: 199

self.topConstraint.constant = newValue;
[self.view setNeedsUpdateConstraints];

[UIView animateWithDuration: animationDuration
                      delay: animationDelay
                    options: UIViewAnimationOptionCurveEaseOut
                 animations: ^(void)
                    {
                        [self.view layoutIfNeeded];
                    }
                 completion: nil];

Upvotes: 0

Bogdan Somlea
Bogdan Somlea

Reputation: 624

have you tried:

 [self.view setNeedsUpdateConstraints];

Upvotes: 4

Related Questions