How do I animate UINavigationBar setBackgroundImage: forBarMetrics:?
ios, ios6, objective-c, uinavigationcontroller
Solution
Tested only on iOS 6
I solved it using Core Animation:
- (void)viewWillAppear:(BOOL)animated
{
[super viewWillAppear:animated];
CATransition *animation = [CATransition animation];
animation.duration = 0.3;
animation.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut];
animation.type = kCATransitionFade;
[self.navigationController.navigationBar.layer addAnimation:animation forKey:nil];
[UIView animateWithDuration:0.3 animations:^{
[self.navigationController.navigationBar setBackgroundImage:[UIImage imageNamed:@"now-playing-nav-bar.png"] forBarMetrics:UIBarMetricsDefault];
}];
}
Do the same in `viewWillDisappear:` and you are good to go.
Problem
I am changing the background image of the navigation bar when a certain controller is pushed. I want to animate that change. I can do so with the following code: ``` [UIView transitionWithView:self.navigationController.navigationBar duration:0.3f options:UIViewAnimationOptionTransitionCrossDissolve animations:^{ [self.navigationController.navigationBar setBackgroundImage:[UIImage imageNamed:@"now-playing-nav-bar.png"] forBarMetrics:UIBarMetricsDefault]; } completion:NULL]; ``` However, this blocks the default push animation applied to the `navigationBarTitle` and `UIBarButtonItem`s. How do I get the background change and the push animations to work together? I would prefer as vanilla a solution as possible. PS: I can't use `tintColor` because the background is textured.