iOS: Presentation controller frame size changes on device orientation

360 views Asked by At

How to keep the presentation controller height same on device orientation change. Below is the code:

    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

There are 1 answers

0
Ankur Prakash On

I think I got the solution. Maybe not very intuitive but it works. Below is the code which will work in my sample.

I am dismissing and representing the controller on "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)
                }
            }
        }
    }