iOS:演示控制器框架尺寸在设备方向上发生变化

时间:2018-11-29 06:10:21

标签: ios device-orientation uipresentationcontroller

如何在设备方向更改时保持演示控制器的高度不变。下面是代码:

    func presentationControllerForPresentedViewController(presented: UIViewController, presentingViewController presenting: UIViewController!, sourceViewController source: UIViewController) -> UIPresentationController? {
        return MyPresentationController(presentedViewController: presented, presentingViewController: presenting)
    }

    class MyPresentationController : UIPresentationController {
        override func frameOfPresentedViewInContainerView() -> CGRect {

        let controllerHeight:CGFloat = 200.0
        let width = UIScreen.main.bounds.width
        let height = UIScreen.main.bounds.height

        return CGRect(x: 0.0, y: height - controllerHeight, width: width, height: controllerHeight)
        }
    }

    let pvc = MyPresentationController()
    pvc.modalPresentationStyle = UIModalPresentationStyle.Custom
    pvc.transitioningDelegate = self

Here is the code sample

1 个答案:

答案 0 :(得分:0)

我想我找到了解决方案。也许不是很直观,但是可以。下面是将在我的示例中使用的代码。

我要解雇并代表“ viewWillTransition”上的控制器

override func viewWillTransition(to size: CGSize, with coordinator: UIViewControllerTransitionCoordinator) {

        if let pvc = presentedViewController {
            pvc.dismiss(animated: true) {
                DispatchQueue.main.async {
// your presentedViewController
                    self.performSegue(withIdentifier: "sampleSegue",
                                      sender: nil)
                }
            }
        }
    }