I tried some stuff and finally ended up with this solution. It doesn't need any extra layers, images or transition views and makes use of the built-in animation of the bar.
Therefore it does not hide any titles and bar buttons that you have placed on your navigation bar. If the nav bar is below a status bar, the status bar's color will also be changed.
This is tested with iOS7, both for simulator as well as real device (iPad)
The code:
Right below your imports do
#define STEP_DURATION 0.01
You can play with this value for a smoother animation
This is the method that you want to call
[self animateNavigationBarFromColor:[UIColor greenColor] toColor:[UIColor blueColor] duration:2.5];
Parameters should be self-explaining (duration is in seconds).
And this is the actual code
- (void)animateNavigationBarFromColor:(UIColor *)fromColor toColor:(UIColor *)toColor duration:(NSTimeInterval)duration
{
NSUInteger steps = duration / STEP_DURATION;
CGFloat fromRed;
CGFloat fromGreen;
CGFloat fromBlue;
CGFloat fromAlpha;
[fromColor getRed:&fromRed green:&fromGreen blue:&fromBlue alpha:&fromAlpha];
CGFloat toRed;
CGFloat toGreen;
CGFloat toBlue;
CGFloat toAlpha;
[toColor getRed:&toRed green:&toGreen blue:&toBlue alpha:&toAlpha];
CGFloat diffRed = toRed - fromRed;
CGFloat diffGreen = toGreen - fromGreen;
CGFloat diffBlue = toBlue - fromBlue;
CGFloat diffAlpha = toAlpha - fromAlpha;
NSMutableArray *colorArray = [NSMutableArray array];
[colorArray addObject:fromColor];
for (NSUInteger i = 0; i < steps - 1; ++i) {
CGFloat red = fromRed + diffRed / steps * (i + 1);
CGFloat green = fromGreen + diffGreen / steps * (i + 1);
CGFloat blue = fromBlue + diffBlue / steps * (i + 1);
CGFloat alpha = fromAlpha + diffAlpha / steps * (i + 1);
UIColor *color = [UIColor colorWithRed:red green:green blue:blue alpha:alpha];
[colorArray addObject:color];
}
[colorArray addObject:toColor];
[self animateWithArray:colorArray];
}
- (void)animateWithArray:(NSMutableArray *)array
{
NSUInteger counter = 0;
for (UIColor *color in array) {
double delayInSeconds = STEP_DURATION * counter++;
dispatch_time_t popTime = dispatch_time(DISPATCH_TIME_NOW, (int64_t)(delayInSeconds * NSEC_PER_SEC));
dispatch_after(popTime, dispatch_get_main_queue(), ^(void){
[UIView animateWithDuration:STEP_DURATION animations:^{
self.navigationController.navigationBar.barTintColor = color;
}];
});
}
}
Drawback: It's not buttery smooth :(