Reputation: 51
I want to hide the navigation bar using animation before I let a UIViewController disappear. Therefore I have implemented the following:
-(void) viewWillDisappear:(BOOL) animated {
[UIView transitionWithView:self.view
duration:UINavigationControllerHideShowBarDuration
options:UIViewAnimationCurveEaseOut
animations:^{
[self.navigationController setNavigationBarHidden:YES];
}
completion:^(BOOL finished){
NSLog(@"animation finished");
}];
[super viewWillDisappear:animated];
}
The problem is that the viewWillDisappear will continue to execute and just return and the whole view will go away before the animation finishes. How can I stop the method from returning before the completion of the animation (where "animation finished" gets printed).
Upvotes: 5
Views: 3070
Reputation: 1031
viewWillDisappear:animated
is in essence a courtesy notification. It's just telling you what is imminent before it happens. You can't actually block or delay the disappearance of the view.
Your best solution would be to create a category on UINavigationController
that creates methods such as (untested):
- (void)pushViewControllerAfterAnimation:(UIViewController *)viewController animated:(BOOL)animated {
[UIView transitionWithView:viewController.view
duration:UINavigationControllerHideShowBarDuration
options:UIViewAnimationCurveEaseOut
animations:^{
[self.navigationController setNavigationBarHidden:NO];
}
completion:^(BOOL finished){
NSLog(@"animation finished");
[self pushViewController:viewController animated:animated];
}];
}
- (void)popViewControllerAfterAnimationAnimated:(BOOL)animated {
[UIView transitionWithView:self.visibleViewController.view
duration:UINavigationControllerHideShowBarDuration
options:UIViewAnimationCurveEaseOut
animations:^{
[self.navigationController setNavigationBarHidden:YES];
}
completion:^(BOOL finished){
NSLog(@"animation finished");
[self popViewControllerAnimated:animated];
}];
}
You could then call these on instead of
- (void)pushViewControllerAfterAnimation:(UIViewController *)viewController animated:(BOOL)animated
and
- (void)popViewControllerAfterAnimationAnimated:(BOOL)animated
respectively.
Upvotes: 2