Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Animating UIView frame size change with autoresizing subviews

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.

like image 507
jjoelson Avatar asked Dec 21 '11 16:12

jjoelson


2 Answers

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
                 }];
like image 127
XJones Avatar answered Oct 12 '22 21:10

XJones


OK, so I figured out the issue after reading the answer to the following question: Animating a UIView frame, subview UIScrollView doesn't always animate

I basically was doing some work after I scheduled the animation which was causing -layoutSubviews to be called on each of my subviews which automatically caused those views to be arranged at the end point of the animation. By scheduling my animation after this work was done I was able to solve my problem.

like image 37
jjoelson Avatar answered Oct 12 '22 23:10

jjoelson