如何在容器转换期间阻止不平衡调用方法?

时间:2015-07-14 10:21:00

标签: ios autolayout

当我在子视图控制器之间转换时,我得到了“开始/结束外观转换的不平衡调用”。

我很确定这是因为我添加了一个子视图控制器,然后transitionFromViewController方法再次添加它。

但我需要添加子视图来设置自动布局的约束。

@IBAction func flipAction(sender: AnyObject) {

    var nextViewController = self.currentViewController == self.frontViewController ? self.backViewController : self.frontViewController

    self.currentViewController.willMoveToParentViewController(nil)
    self.addChildViewController(nextViewController)

    nextViewController.view.frame = self.containerView.frame
    self.containerView.addSubview(nextViewController.view)
    nextViewController.view.setTranslatesAutoresizingMaskIntoConstraints(false)

    self.containerView.addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("H:|-[back]-|", options: NSLayoutFormatOptions.allZeros, metrics: nil, views: ["back": nextViewController.view]))
    self.containerView.addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("V:|-[back]-|", options: NSLayoutFormatOptions.allZeros, metrics: nil, views: ["back": nextViewController.view]))

    self.transitionFromViewController(self.currentViewController, toViewController: nextViewController, duration: 1, options: UIViewAnimationOptions.TransitionFlipFromRight, animations: { () -> Void in

    }) { (success: Bool) -> Void in

        self.currentViewController.removeFromParentViewController()
        nextViewController.didMoveToParentViewController(self)
        self.currentViewController = nextViewController
    }
}

如何防止不平衡调用方法?

0 个答案:

没有答案