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

相关文章

UITabBarController 是 iOS 中用于管理和显示选项卡界面的一...
UITableView的重用机制避免了频繁创建和销毁单元格的开销,使...
Objective-C中,类的实例变量(instance variables)和属性(...
从内存管理的角度来看,block可以作为方法的传入参数是因为b...
WKWebView 是 iOS 开发中用于显示网页内容的组件,它是在 iO...
OC中常用的多线程编程技术: 1. NSThread NSThread是Objecti...