关于转场动画中“Unbalanced calls to begin/end appearance transitions for \”警告的解决

Tips

最近学习转场动画时,参考了喵神的这篇文章:WWDC 2013 Session笔记 - iOS7中的ViewController切换,文中有这样一段示例代码:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
//ContainerVC.m

[self addChildViewController:toVC];
[fromVC willMoveToParentViewController:nil];
[self.view addSubview:toVC.view];

__weak id weakSelf = self;
[self transitionFromViewController:fromVC
toViewController:toVC duration:0.3
options:UIViewAnimationOptionTransitionCrossDissolve
animations:^{}
completion:^(BOOL finished) {
[fromVC.view removeFromSuperView];
[fromVC removeFromParentViewController];
[toVC didMoveToParentViewController:weakSelf];
}];

是iOS7以前的转场动画的实现方案,实验这段代码时,控制台报了如下警告:

1
Unbalanced calls to begin/end appearance transitions for <UIViewController: 0x7fc28fc1c080>.

通过查找原因,解决方案如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
//ContainerVC.m

[self addChildViewController:toVC];
[fromVC willMoveToParentViewController:nil];

__weak id weakSelf = self;
[self transitionFromViewController:fromVC
toViewController:toVC duration:0.3
options:UIViewAnimationOptionTransitionCrossDissolve
animations:^{}
completion:^(BOOL finished) {
[fromVC removeFromParentViewController];
[toVC didMoveToParentViewController:weakSelf];
}];

只是去掉了关于vc.view的操作,解释在这里:
Unbalanced calls to begin/end appearance transitions for \<UIViewController>

Turns out transitionFromViewController:toViewController:duration:options:animations:completion: also adds the view.

This method adds the second view controller’s view to the view hierarchy and then performs the animations defined in your animations block. After the animation completes, it removes the first view controller’s view from the view hierarchy.
Which means the addSubview needs to be adjusted accordingly.

也就是说transitionFromViewController:toViewController:duration:options:animations:completion:这个方法内部已经做了关于vc.view的管理,我们不需要再自行添加和移除对应的view了。


本文作者:霖溦
本文链接:https://kukumalucn.github.io/blog/2018/12/05/关于转场动画中“Unbalanced-calls-to-begin-end-appearance-transitions-for-UIViewController-”警告的解决/
版权声明:本博客所有文章除特别声明外,均采用 CC BY-NC-ND 4.0 许可协议。转载请注明出处!

坚持原创技术分享,您的支持将鼓励我继续创作!