I appreciate the answer provided, but I think it would be nice to take it a bit further.
The basic block animation from the documentation
[containerView layoutIfNeeded]; // Ensures that all pending layout operations have been completed[UIView animateWithDuration:1.0 animations:^{ // Make all constraint changes here [containerView layoutIfNeeded]; // Forces the layout of the subtree animation block and then captures all of the frame changes}];
but really this is a very simplistic scenario. What if I want to animate subview constraints via the updateConstraints
method?
An animation block that calls the subviews updateConstraints method
[self.view layoutIfNeeded];[self.subView setNeedsUpdateConstraints];[self.subView updateConstraintsIfNeeded];[UIView animateWithDuration:1.0f delay:0.0f options:UIViewAnimationOptionLayoutSubviews animations:^{ [self.view layoutIfNeeded];} completion:nil];
The updateConstraints method is overridden in the UIView subclass and must call super at the end of the method.
- (void)updateConstraints{ // Update some constraints [super updateConstraints];}
The AutoLayout Guide leaves much to be desired but it is worth reading. I myself am using this as part of a UISwitch
that toggles a subview with a pair of UITextField
s with a simple and subtle collapse animation (0.2 seconds long). The constraints for the subview are being handled in the UIView subclasses updateConstraints methods as described above.