ios – 为什么在使用手动视图包含时在错误的时间调用viewWillDisappear?

前端之家收集整理的这篇文章主要介绍了ios – 为什么在使用手动视图包含时在错误的时间调用viewWillDisappear?前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
这就是我使用包含API的方式.根据 docs是正确的.
[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];
}];

我希望在动画开始之前调用viewWillDisappear,并在动画完成后调用viewDidDisappear.但是,在动画完成后,它们都会快速连续调用.

移动[self.childViewController.view removeFromSuperview];到动画块修复了这个,但代码看起来错了:

[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];
}];

有谁知道在正确的时间调用viewWillDisappear的正确方法是什么?

解决方法

答案是使用 – 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.

更正后的代码

[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];
}];

猜你在找的iOS相关文章