容器视图控制器 - 对 begin/end 外观转换的不平衡调用
Container View Controller - Unbalanced calls to begin/end appearance transitions
我正在尝试开发 Apple 文档中显示的 Container View Controller。
现在我在 viewDidAppear
:
中有简单的初始化代码
presentedVC = self.storyboard!.instantiateViewControllerWithIdentifier(Storyboard.yesNoControllerID)
self.addChildViewController(presentedVC)
presentedID = Storyboard.yesNoControllerID
presentedVC.view.frame = containerView.bounds
self.containerView.addSubview(presentedVC.view)
presentedVC.didMoveToParentViewController(self)
我已经实现了交换方法,就像在 Apple 文档中一样:
private func exchangeVC(withVC viewController: UIViewController){
presentedVC.willMoveToParentViewController(nil)
self.addChildViewController(viewController)
viewController.view.frame = newViewStartFrame
let endFrame = oldViewEndFrame
self.containerView.addSubview(viewController.view)
self.transitionFromViewController(presentedVC, toViewController: viewController, duration: 0.25, options: UIViewAnimationOptions.CurveLinear, animations: {
viewController.view.frame = self.presentedVC.view.frame
self.presentedVC.view.frame = endFrame
}) { (finished) in
self.presentedVC.view.removeFromSuperview()
self.presentedVC.removeFromParentViewController()
viewController.didMoveToParentViewController(self)
self.presentedVC = viewController
}
}
然后,我有一个简单调用的按钮:
let controller = self.storyboard!.instantiateViewControllerWithIdentifier(presentedID)
exchangeVC(withVC: controller)
使用此代码,我的控制器在按下按钮时在屏幕上显示动画。但是在动画结束时我得到:
Unbalanced calls to begin/end appearance transitions for
UIViewController: 0x7aecf730.
你能告诉我我做错了什么吗?如何摆脱这个 error/warning?
我遇到了类似的问题。通常,当您尝试呈现相同的视图控制器而不首先关闭它时,会出现该警告,但转换方法应该为您处理所有这些。我最终使用
修复了它
UIView.animate(withDuration:animations:completion:)
而不是
UIViewController.transition(from:to:duration:options:animations:completion:)
如你所见。当然,我也必须手动添加和删除子视图。我不确定为什么此更改会起作用,但我最好的猜测是发出该警告的 UIViewController
转换方法有问题。幸运的是修复真的很容易。
这是一个容易犯的错误,但 Apple 的 transition(from:to:duration:options:animations:completion:)
文档指出:
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.
您收到 Unbalanced calls to begin/end appearance transitions
警告是因为您添加了一次子视图:
self.containerView.addSubview(viewController.view)
... 然后在调用时再次添加它 transition(from:to:duration:options:animations:completion:)
删除对 addSubview
的调用将解决问题。
我在 iPhone 上的按钮点击处理程序中看到了这个不平衡的调用错误。将其追溯到 dismiss(animated: no) 然后是 present(.. animated: no)
解决方案是在关闭调用的完成处理程序中执行 present。
我正在尝试开发 Apple 文档中显示的 Container View Controller。
现在我在 viewDidAppear
:
presentedVC = self.storyboard!.instantiateViewControllerWithIdentifier(Storyboard.yesNoControllerID)
self.addChildViewController(presentedVC)
presentedID = Storyboard.yesNoControllerID
presentedVC.view.frame = containerView.bounds
self.containerView.addSubview(presentedVC.view)
presentedVC.didMoveToParentViewController(self)
我已经实现了交换方法,就像在 Apple 文档中一样:
private func exchangeVC(withVC viewController: UIViewController){
presentedVC.willMoveToParentViewController(nil)
self.addChildViewController(viewController)
viewController.view.frame = newViewStartFrame
let endFrame = oldViewEndFrame
self.containerView.addSubview(viewController.view)
self.transitionFromViewController(presentedVC, toViewController: viewController, duration: 0.25, options: UIViewAnimationOptions.CurveLinear, animations: {
viewController.view.frame = self.presentedVC.view.frame
self.presentedVC.view.frame = endFrame
}) { (finished) in
self.presentedVC.view.removeFromSuperview()
self.presentedVC.removeFromParentViewController()
viewController.didMoveToParentViewController(self)
self.presentedVC = viewController
}
}
然后,我有一个简单调用的按钮:
let controller = self.storyboard!.instantiateViewControllerWithIdentifier(presentedID)
exchangeVC(withVC: controller)
使用此代码,我的控制器在按下按钮时在屏幕上显示动画。但是在动画结束时我得到:
Unbalanced calls to begin/end appearance transitions for UIViewController: 0x7aecf730.
你能告诉我我做错了什么吗?如何摆脱这个 error/warning?
我遇到了类似的问题。通常,当您尝试呈现相同的视图控制器而不首先关闭它时,会出现该警告,但转换方法应该为您处理所有这些。我最终使用
修复了它UIView.animate(withDuration:animations:completion:)
而不是
UIViewController.transition(from:to:duration:options:animations:completion:)
如你所见。当然,我也必须手动添加和删除子视图。我不确定为什么此更改会起作用,但我最好的猜测是发出该警告的 UIViewController
转换方法有问题。幸运的是修复真的很容易。
这是一个容易犯的错误,但 Apple 的 transition(from:to:duration:options:animations:completion:)
文档指出:
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.
您收到 Unbalanced calls to begin/end appearance transitions
警告是因为您添加了一次子视图:
self.containerView.addSubview(viewController.view)
... 然后在调用时再次添加它 transition(from:to:duration:options:animations:completion:)
删除对 addSubview
的调用将解决问题。
我在 iPhone 上的按钮点击处理程序中看到了这个不平衡的调用错误。将其追溯到 dismiss(animated: no) 然后是 present(.. animated: no)
解决方案是在关闭调用的完成处理程序中执行 present。