popoverPresentationController(_:willRepositionPopoverTo:in :)试图在不同的方向上改变popUp的大小

时间:2017-10-26 12:04:29

标签: ios swift uipopovercontroller

我有一个弹出窗口,我试图在方向改变时改变宽度。当popUp以当前方向显示时,我获得了我想要的宽度,但是当我从.portrait切换时 - > .landscape我没有得到我想要的宽度。例如,它保留了.portrait。

我阅读了有关popoverPresentationController的文档(_:willRepositionPopoverTo:in:)

但我无法弄清楚它是如何运作的。 实际上,当我改变方向时,它甚至都没有调用。我使用的其余UIPopoverPresentationControllerDelegate工作正常。

这是改变PopUp大小的正确方法,还是我应该使用别的东西?

有任何帮助吗?非常感谢

func popoverPresentationController(_ popoverPresentationController: UIPopoverPresentationController, willRepositionPopoverTo rect: UnsafeMutablePointer<CGRect>, in view: AutoreleasingUnsafeMutablePointer<UIView>) {

        if popoverPresentationController.presentingViewController is PopUpTextPickerViewController{
            let viewFrame = popoverPresentationController.presentingViewController.view.frame

            let newRect = CGRect(x: viewFrame.origin.x, y: viewFrame.origin.y, width: self.view.bounds.width, height: 100)
            let newView = UIView(frame: newRect)
            rect.pointee =  newRect
            view.pointee = newView

        }
        print("popoverPresentation:willRepositionPopOverTo")
    }

    //Shows the PopUpTextPickerViewController on the screen
    @IBAction func fontButtonPressed(_ sender: UIBarButtonItem) {

        subscribeToNotifications(notification: .popUpTextPickerViewController)
        let fontController = storyboard?.instantiateViewController(withIdentifier: "popUpTextPickerViewController") as! PopUpTextPickerViewController
        fontController.fontName = self.fontName
        fontController.fontSize = self.fontSize
        fontController.modalPresentationStyle = UIModalPresentationStyle.popover
        fontController.popoverPresentationController?.delegate = self
        fontController.popoverPresentationController?.barButtonItem = fontButton
        fontController.popoverPresentationController?.backgroundColor = .clear
        fontController.popoverPresentationController?.sourceView = self.view
        fontController.preferredContentSize = CGSize(width: self.view.bounds.width, height: 100)
        present(fontController, animated: true, completion: nil)
    }

1 个答案:

答案 0 :(得分:0)

尝试在父视图控制器中实现以下代码。在更改显示的视图控制器视图的大小之前,UIKit会调用此方法-

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

    super.viewWillTransition(to: size, with: coordinator)

    if let myVC = self.presentedViewController as? MyViewController {
            myVC.preferredContentSize = CGSize(width: {yorWidth}, height: {yourHeight})
    }
}