Animating UIView frame size change with autoresizing subviews

jjoelson picture jjoelson · Dec 21, 2011 · Viewed 14.7k times · Source

I have a UIView which contains two subviews. I would like to change the superview's frame size with an animation and have the subviews' sizes change in accordance with their autoresize masks.

[UIView beginAnimations:@"Resize" context:nil];
[UIView setAnimationDuration:1.0];
CGRect frame = self.myView.frame;
frame.size.height += 30.0;
self.myView.frame = frame;
[UIView commitAnimations];

The view self.myView is resized over 1 second as expected, but the subviews are resizing immediately. Does anyone know why this is happening and how I might make the subviews animate their resizing as well?

From my googling around, I think it might have something to with the contentMode property. Unfortunately, I'm not really clear as to what that property does. Any help would be much appreciated.

Answer

XJones picture XJones · Dec 21, 2011

It would have been hard for someone else to give you the correct answer w/o seeing your code though I would have asked you what you are doing after [UIView commitAnimations]. Nevertheless, glad you figured it out. I suggest you use the block animations. They make this type of mistake much easier to avoid by using a completion block. Example:

[UIView animateWithDuration:1.0
                 animations:^{
                     CGRect frame = self.myView.frame;
                     frame.size.height += 30.0;
                     self.myView.frame = frame;
                 }
                 completion:^(BOOL finished){
                     // whatever you need to do when animations are complete
                 }];