Animate a UINavigationBar's barTintColor

ios7, objective-c, uinavigationbar, uinavigationcontroller

Solution

You can add extra animations that match the timing and animation curve of the view controller transition using UIViewControllerTransitionCoordinator.

A view controller's `transitionCoordinator` will be set after a view controller's animation has started (so in `viewWillAppear` of the presented view controller). Add any extra animations using `animateAlongsideTransition:completion:` on the transition coordinator.

An example:

[[self transitionCoordinator] animateAlongsideTransition:^(id<UIViewControllerTransitionCoordinatorContext> context) {
    self.navigationController.navigationBar.translucent = NO;
    self.navigationController.navigationBar.barStyle = UIBarStyleBlack;
    self.navigationController.navigationBar.tintColor = [UIColor whiteColor];
    self.navigationController.navigationBar.barTintColor = [UIColor redColor];
} completion:nil];

Problem

The app I'm working on changes the `barTintColor` of its navigation bar when pushing new view controllers. Right now we set that colour in the destination view controller's `viewWillAppear:`method, but we have a few issues with that. With the way we're doing this right now, the navigation bar's colour changes abruptly, while the rest of the bar content animates as usual. What I'd like is for the bar to fade between the source and destination colour. Is there any way to achieve this with public Cocoa Touch APIs?

Original source