Search code examples
iosobjective-canimationuinavigationcontrollerback

iOS: Customize Back Button Transition Animation


I have a view controller (inherits from UIViewController) that has custom logic for presenting data. It can init a new view controller (of the same type) and display it using [self.navigationController pushViewController:next animated:NO] (where next is the newly instantiated controller), creating an experience of a "stack of views", using the navigation controller default back button to navigate up the stack.

Now, before calling pushViewController:animated: I am able to customize the transition animation by calling [self.navigationController.view.layer addAnimation:animation forKey:nil]. This works wonderfully for "going down the stack" of views.

But since navigation up the stack is done the default way with a default back button, the transition used is the default slide-fro-left animation which doesn't match my customized animation.

How can I customize this transition animation?

EDIT:

I took the ideas given here and came up with:

UIBarButtonItem *back = [[UIBackButtonItem alloc] initWithTitle:@"\U000025C0\U0000FE0E" style:UIBarButtonItemStylePlain target:self action:@selector(back:)];
[self.navigationItem setLeftBarButtonItem:back]

And the transition:

- (IBAction)back:(id)sender {
    CATransiotion *animation = [CATransition animation];
    animation.duration = 0.3f;
    amimation.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionLinear];
    animation.type = kCATransitionMoveIn;
    [self.navigationController.view.layer addAnimation:animation forKey:nil];
    [self.navigationController popViewControllerAnimated:NO];
}

The only downside is the appearance of the new back button which I'm willing to live with.

Thanks for the answers :)


Solution

  • I guess you have to make your custom back button, and attach your animations to it, eg.

    - (IBAction) back:(id)sender
    {       
        [UIView transitionWithView:self.navigationController.view
                          duration:0.5
                           options:UIViewAnimationOptionTransitionFlipFromLeft
                        animations:^{ [self.navigationController popViewControllerAnimated:NO]; }
                        completion:NULL];
    }