This is how I am using the containment API. According to the docs is its correct.
[self.childViewController willMoveToParentViewController:nil];
[UIView animateWithDuration:0.25 animations:^{
self.childViewController.view.frame = ... // View Animation
} completion:^(BOOL finished) {
[self.childViewController.view removeFromSuperview]; // triggers `viewWillDisappear`
[self.childViewController removeFromParentViewController];
}];
I expect viewWillDisappear
to be called before the animation begins and viewDidDisappear
to be called after the animation completes. However, they are both called in quick succession after the animation is complete.
Moving [self.childViewController.view removeFromSuperview];
to the animation block fixes this, but the code looks wrong:
[self.childViewController willMoveToParentViewController:nil];
[UIView animateWithDuration:0.25 animations:^{
self.childViewController.view.frame = ... // View Animation
[self.childViewController.view removeFromSuperview]; // triggers `viewWillDisappear`
} completion:^(BOOL finished) {
[self.childViewController removeFromParentViewController];
}];
Does anyone know what the proper way to get viewWillDisappear
to be called at the correct time is?
The answer was to use – beginAppearanceTransition:animated:
& endAppearanceTransition
If you are implementing a custom container controller, use this method to tell the child that its views are about to appear or disappear. Do not invoke viewWillAppear:, viewWillDisappear:, viewDidAppear:, or viewDidDisappear: directly.
The corrected code:
[self.childViewController willMoveToParentViewController:nil];
[self.childViewController beginAppearanceTransition:NO animated:YES];
[UIView animateWithDuration:0.25 animations:^{
self.childViewController.view.frame = ...
} completion:^(BOOL finished) {
[self.childViewController.view removeFromSuperview];
[self.childViewController removeFromParentViewController];
[self.childViewController endAppearanceTransition];
}];
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With