Custom form sheet size and rotation

On my ipad, to set a custom form sheet size, I'm setting the preferred content size by tapping into the segue that presents it:

    override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
        if segue.identifier == "SaleSegue" {
            let horizontalPadding = 40
            let verticalPadding = 30
            let width = view.frame.size.width - CGFloat(horizontalPadding * 2)
            let height = view.frame.size.height - CGFloat(verticalPadding * 2)
            let navigationController: UINavigationController = (segue.destination as? UINavigationController)!
            navigationController.preferredContentSize = CGSize(width: width, height: height)
        }
    }


And it works as expected, however, what I'm having trouble with is how to get the desired results when screen rotates. The fact that it doesn't leads me to believe this is the absolute wrong approach. I've tried overwriting preferredContentSize inside of the rootview controller of the navigation controller, however, the modal just begins to shrink as in, the width and height of its view is changing for some reason I don't understand.


My question is more so if anyone here has done this in the past and what's the proper way of achieving this?


I've attached a sample project outlining the issue:

h ttps://www.dropbox.com/s/6ik2p6bwg7962sz/Example.zip?dl=0

If I remember well, view frame is not changed after rotation.

add a print(view.frame.size) in the prepare() to check this.


Then you need to call viewWillTransition and adapt the frame.

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

        super.viewWillTransition(to: size, with: coordinator)
        let formerSize = self.view.frame.size
        let newSize = GSize(width: formerSize.height, height: formerSize.width)
        self.view.frame = CGRect(origin: CGPoint.zero, size: newSize)
    }


See details some here:

h ttps://stackoverflow.com/questions/40262481/uiview-frame-not-updating-after-orientation-change

Thanks for your response Claude31. Unfortunately I'm having trouble following what you're suggesting (or getting it to work) and the link you suggested wasn't much help either.


I've attached a sample project outlining the issue:

h ttps://www.dropbox.com/s/6ik2p6bwg7962sz/Example.zip?dl=0


Perhaps this will help you let me know how this can be done? Thanks for any help.

Custom form sheet size and rotation
 
 
Q