Animating UIView frame size change with autoresizing subviews

14,928

Solution 1

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.

Solution 2

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
                 }];
Share:
14,928
jjoelson
Author by

jjoelson

Updated on June 30, 2022

Comments

  • jjoelson
    jjoelson almost 2 years

    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.

  • jjoelson
    jjoelson over 12 years
    Unfortunately, the need to be compatible with older versions of iOS rules out block animations. Thanks for the tip though.
  • XJones
    XJones over 12 years
    makes sense. though the installed base of < 4.x is pretty tiny these days.