2

I'm getting Unbalanced calls to begin/end appearance transitions error when i try to present view controller which is previously added as child controller, how to resolve this?

Unbalanced calls to begin/end appearance transitions

class VideoViewController: UIViewController {

}


class BigPlayerVC: UIViewController {
    let videoVC = VideoViewController()

    override func viewDidLoad() {
        super.viewDidLoad()
        addChild(videoVC)
        view.addSubview(videoVC.view)
        videoVC.view.translatesAutoresizingMaskIntoConstraints = false
    //  view.constrainViewEqual(videoReactController.playerView, top: 0, bottom: 0, left: 0, righta: 0)
        videoVC.didMove(toParent: self)
    }

    @objc func onBTNClick() {
        videoVC.willMove(toParent: nil)
        videoVC.view.removeFromSuperview()
        videoVC.view.translatesAutoresizingMaskIntoConstraints = true
        videoVC.removeFromParent()
        self.present(videoVC, animated: false, completion: nil)
    }
}
SPatel
  • 4,768
  • 4
  • 32
  • 51

2 Answers2

2

I have one temporary solution: Just move present method in DispatchBlock with delay like below:

DispatchQueue.main.asyncAfter(deadline: DispatchTime.now() + 0.1) {
    self.present(videoVC, animated: false, completion: nil)
} 
SPatel
  • 4,768
  • 4
  • 32
  • 51
0

I got exactly the same problem as you. You should listen on viewDidDisappeared in videoVC and then present the view controller. Method viewDidDisappeared is called immediately, so you won't notice performance issues.

   @objc func onBTNClick() {
        videoVC.willMove(toParent: nil)
        videoVC.view.removeFromSuperview()
        videoVC.removeFromParent()
        videoVC.onViewDidDisappeared = {
             [unowned self, videoVC] in
             self.present(videoVC, animated: false, completion: nil)
             videoVC.onViewDidDisappeared = nil
        }
    }
final class YourViewController: UIViewController {
    var onViewDidDisappeared: (() -> Void)? = nil

    override func viewDidDisappear(_ animated: Bool) {
        super.viewDidDisappear(animated)
        onViewDidDisappeared?()
    }

[Edit] It's not only to remove unbalanced calls, but also remove UICollectionView unfocus issue that might appear on AppleTV

Szu
  • 2,254
  • 1
  • 21
  • 37