Popover旋转时查看位置到错误的位置

问题描述 投票:0回答:1

我可能会尝试将弹出框居中,并使其在旋转后仍居中。研究几乎回答了我的问题。但是,我注意到一个非常奇怪的问题:当设备在纵向时启动弹出窗口时,它首先会居中,但是,如果我旋转到风景方向,然后再次将其旋转回纵向,则它是比中心位置高一点。我已经通过屏幕截图制作了图片,以表明

comparison between before and after

左侧的屏幕截图是正确的位置,首先启动弹出窗口时会出现,右侧的屏幕快照是错误的位置,您可以通过我在它们之间绘制的线条清楚地看到差异。

这是我用来使弹出窗口居中的代码

override func viewWillTransition(to size: CGSize, with coordinator: UIViewControllerTransitionCoordinator) {
        if let controller = self.presentedViewController as? PopoverController {
            controller.popoverPresentationController?.sourceRect = CGRect(x: size.width / 2,
                                                                          y: size.height / 2,
                                                                          width: 0, height: 0)
            let globalPoint = controller.view.superview?.convert(controller.view.frame.origin, to: nil)
            controller.label.text = "x: \(globalPoint!.x), y: \(globalPoint!.y)"
            self.view.layoutIfNeeded()
        }
    }

    override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
        if segue.identifier == "popoversegue" {
            let controller = segue.destination
            controller.popoverPresentationController!.delegate = self
            controller.popoverPresentationController!.sourceRect = CGRect(x: view.center.x, y: view.center.y,
                                                                          width: 0, height: 0)
            controller.popoverPresentationController?.sourceView = self.view
            controller.preferredContentSize = CGSize(width: 500, height: 600)
            controller.popoverPresentationController!.permittedArrowDirections = UIPopoverArrowDirection(rawValue: 0)
        }
    }

此外,我已经意识到问题仅发生在纵向,弹出窗口始终很好地位于横向位置的中心。这里可能是什么问题?

ios swift uipopovercontroller uipopoverpresentationcontroller
1个回答
0
投票

使您的类符合“ UIPopoverPresentationControllerDelegate”

© www.soinside.com 2019 - 2024. All rights reserved.