How to wait for the animation to end in viewDidDisappear?

I want to hide the navigation bar with animation before I let the UIViewController disappear. So I did 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 viewWillDisappear will continue to execute and just return, and the whole view will disappear before the animation finishes. How can I stop a method from returning to completion of the animation (where "animation completed" ends).

+5
source share
1 answer

viewWillDisappear:animatedis essentially a courtesy notice. It just tells you what is inevitable before this happens. You cannot block or delay the disappearance of a view.

UINavigationController, , (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];
                    }];
}

- (void)pushViewControllerAfterAnimation:(UIViewController *)viewController animated:(BOOL)animated

- (void)popViewControllerAfterAnimationAnimated:(BOOL)animated

.

+2

All Articles