Transition between view controllers and rotation in iOS

ios, ios5, iphone, uiinterfaceorientation, uiviewcontroller

Solution

As soon as you call `addChildViewController:` you are now a View Controller Container Implementer. This means you do have to do a little more work than a standard presentation call like `presentViewController..`. This includes dealing with the frames of the views of the controllers you add as children, as your question suggests you might have expected.

For example, to implement a super basic example container, that just shows each child full screen, you could do something like this.

-(void)swapChildVCFrom:(UIViewController *)from to:(UIViewController *)to{
    [self addChildViewController:to];
    [from willMoveToParentViewController:nil];

    // Adjust the new child view controller's view's frame
    // For example here just set it to fill the parent view
    to.view.frame = self.view.bounds;

    [self transitionFromViewController:from
                      toViewController:to
                              duration:1.0
                               options:UIViewAnimationOptionTransitionFlipFromLeft
                            animations:nil
                            completion:^(BOOL b){
                                [to didMoveToParentViewController:self];
                                [from.view removeFromSuperview];
                                [from removeFromParentViewController];
                            }];
}

Problem

Consider a container view controller with two child view controllers (A and B), both added with `addChildViewController:`. Then: - `A.view` is added to the container view - B is displayed by doing `transitionFromViewController` from A to B. B receives `viewWillLayoutSubviews` and all is good with the world. - The device rotates while displaying B. Only B receives the rotation calls (`willRotateToInterfaceOrientation:` et all). - A is displayed by doing `transitionFromViewController` from B to A. A doesn't receive `viewWillLayoutSubviews` and thus the layout is broken. Is this the expected behavior? If not, what might I be doing wrong? If yes, what should I do to notify A of the rotation change while displaying B?

Original source