Reputation: 2530
I'm building an app whereas I'm facing a strange issue with constraints.
I'm trying to animate a height constraint attached to a UIView
in storyboard, but the simulator displays some weird behaviour. To debug, I created a completely new, iOS 9.2 project and added a UIButton
, set up a horizontal pin and superview top constraint to it, linked the top constraint to my header file and the button itself to an IBAction
.
When calling this code alone:
- (IBAction)myAction:(id)sender {
self.topConstraint.constant += 150.0f;
}
The button snaps 150 points down, even without calling [self.view layoutIfNeeded];
.
However, when I'm using this code:
- (IBAction)myAction:(id)sender {
self.topConstraint.constant += 150.0f;
[self.view setNeedsUpdateConstraints];
[UIView animateWithDuration:1 animations:^{
[self.view layoutIfNeeded];
}];
}
The button slides down.
How come it snaps there without me calling layoutIfNeeded
when there's no animation block? I find this very strange.
Here's my header file:
#import <UIKit/UIKit.h>
@interface ViewController : UIViewController
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *topConstraint;
@end
And my top NSLayoutConstraint
setup:
And the UIViewController
in interface builder:
Upvotes: 1
Views: 376
Reputation: 3141
Calling layoutIfNeeded on a view merely forces an update of the view early, rather than waiting for an update through the drawing cycle, when you animate your constraint you are placing the layoutIfNeeded inside the animation block thus animating the change, in the 2nd instance where you do not call layoutIfNeeded, the drawing cycle comes around and updates the view without animation.
Upvotes: 1