未调用UIViewControllerTransitioningDelegate方法

时间:2016-11-11 10:11:39

标签: ios swift

我正在尝试使用自定义转换实现自定义弹出窗口,但我的委托方法根本没有被调用。这是我的过渡代表:

public final class ModalTransitioningDelegate: NSObject, UIViewControllerTransitioningDelegate {

    public func presentationController(forPresented presented: UIViewController, presenting: UIViewController?, source: UIViewController) -> UIPresentationController? {
        let controller = ModalPresentationController(presentedViewController: presented, presenting: presenting)

        return controller
    }

    public func animationController(forPresented presented: UIViewController, presenting: UIViewController, source: UIViewController) -> UIViewControllerAnimatedTransitioning? {
        return ModalAnimationPresenter()
    }

    public func animationController(forDismissed dismissed: UIViewController) -> UIViewControllerAnimatedTransitioning? {
        return ModalAnimationDissmiser()
    }
}

这是我的弹出式视图控制器:

class StopWorkoutViewController: UIViewController {

    required init?(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)
        commonInit()
    }

    override init(nibName nibNameOrNil: String?, bundle nibBundleOrNil: Bundle?) {
        super.init(nibName: nibNameOrNil, bundle: nibBundleOrNil)
        commonInit()
    }

    func commonInit() {

        let transitioner = ModalTransitioningDelegate()
        modalPresentationStyle = .custom
        transitioningDelegate = transitioner
    }
}

这是我呈现弹出窗口的方式:

@IBAction func test(_ sender: Any) {
    let popup = UIStoryboard(name: "Popups", bundle: nil).instantiateInitialViewController() as! StopWorkoutViewController
    present(popup, animated: true, completion: nil)
}

这是IB中的视图控制器:

Popup ViewController

弹出窗口显示,但是全屏。

3 个答案:

答案 0 :(得分:3)

这是错误的

 func commonInit() {

        let transitioner = ModalTransitioningDelegate()
        modalPresentationStyle = .custom
        transitioningDelegate = transitioner
    }

由于您要为StopWorkoutViewController的过渡设置动画。您需要将过渡委托设置为

 @IBAction func test(_ sender: Any) {
    let transitioner = ModalTransitioningDelegate()
    let popup = UIStoryboard(name: "Popups", bundle: nil).instantiateInitialViewController() as! StopWorkoutViewController
    popup.transitioningDelegate = transitioner
    present(popup, animated: true, completion: nil)
}

答案 1 :(得分:1)

问题是transitioningDelegate是一个弱属性,因此分配给它的类在转换有机会运行之前就会被释放。有关如何在调试器中捕获此内容的示例,请参阅我的回答here

答案 2 :(得分:0)

在我的情况下,它是关于present(_:animated :)和dismiss(animated:completion :)方法,其中动画设置为false