gpt4 book ai didi

ios - 为什么在使用手动 View 包含时在错误的时间调用 viewWillDisappear?

转载 作者:可可西里 更新时间:2023-11-01 03:28:44 28 4
gpt4 key购买 nike

这就是我使用 containment 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]; 移动到动画 block 修复了这个问题,但代码看起来不对:

[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 - 为什么在使用手动 View 包含时在错误的时间调用 viewWillDisappear?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25021072/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com