1
votes

I'm attempting to animate the navigation bar when performing a custom segue. The navigation bar is controlled by a Navigation Controller. The following code allows the embedded view to fade and zoom into view, though the navigation bar remains unanimated. How the changes in navigation bar be animated?

#import "MDAZoomSegue.h"

@implementation MDAZoomSegue

- (void)perform {

    UIViewController *sourceViewController = (UIViewController *) self.sourceViewController;
    UIViewController *destinationViewController = (UIViewController *) self.destinationViewController;

    [sourceViewController.view addSubview:destinationViewController.view];
    [destinationViewController.view setFrame:sourceViewController.view.window.frame];
    [destinationViewController.view setTransform:CGAffineTransformMakeScale(1.2,1.2)];
    [destinationViewController.view setAlpha:0];

    [UIView animateWithDuration:0.5
                          delay:0.0
                        options:UIViewAnimationCurveEaseOut
                     animations:^{
                         [destinationViewController.view setTransform:CGAffineTransformMakeScale(1.0,1.0)];
                         [destinationViewController.view setAlpha:1.0];
                     }
                     completion:^(BOOL finished){
                         [destinationViewController.view removeFromSuperview];
                         [sourceViewController.navigationController pushViewController:destinationViewController animated:NO];
                     }];

}

@end
1

1 Answers

0
votes

If you take into account the view hierarchy for the controller, you'll realise that the navigationController's view is the superview for the controller's view. Hence, if you want to shift the navigation bar for the destinationViewController, rather than acting on destinationViewController.view, use destinationViewController.navigationController.view.

In other words, changing perform to

- (void)perform {

    UIViewController *sourceViewController = (UIViewController *) self.sourceViewController;
    UIViewController *destinationViewController = (UIViewController *) self.destinationViewController;

    [sourceViewController.view addSubview:destinationViewController.navigationController.view];
    [destinationViewController.navigationController.view setFrame:sourceViewController.view.window.frame];
    [destinationViewController.navigationController.view setTransform:CGAffineTransformMakeScale(1.2,1.2)];
    [destinationViewController.navigationController.view setAlpha:0];

    [UIView animateWithDuration:0.5
                          delay:0.0
                        options:UIViewAnimationCurveEaseOut
                     animations:^{
                         [destinationViewController.navigationController.view setTransform:CGAffineTransformMakeScale(1.0,1.0)];
                         [destinationViewController.navigationController.view setAlpha:1.0];
                     }
                     completion:^(BOOL finished){
                         [destinationViewController.navigationController.view removeFromSuperview];
                         [sourceViewController.navigationController pushViewController:destinationViewController animated:NO];
                     }];

}

@end

I believe you'll see the navigation bar animating as well.