Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to wait for an animation to finish in viewDidDisappear?

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).

like image 747
Ed Taylor Avatar asked Nov 04 '22 06:11

Ed Taylor


1 Answers

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.

like image 191
Josh Hudnall Avatar answered Nov 09 '22 11:11

Josh Hudnall