简体   繁体   English

Popoverview并非以iPad旋转为中心

[英]Popoverview is not centred on iPad rotation

In following code popover on iPad is not centred for second alert (cities) when iPad is rotated. 在下面的代码中,旋转iPad时,iPad上的弹出窗口不在第二警报(城市)的中心。 It works fine for first alert (countries) though. 不过,它对于第一个警报(国家/地区)也可以正常工作。 I've printed the values and it's same for both alerts. 我已经打印了值,两个警报都相同。 It seems that despite having correct value for coordinates it doesn't present it in centre on device rotation for second alert. 似乎,尽管坐标值正确,但它并没有在设备旋转的中心显示它,以发出第二次警报。

Why it doesn't show in centre for second alert? 为什么第二个警报没有显示在中心? How to fix it? 如何解决?

class MyVC: UIViewController {

    var popoverController:UIPopoverPresentationController?

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

        self.tableView.reloadData()

        if self.popoverController != nil {
            popoverController?.sourceView = self.view
            print("viewWillTransition():width: x: \(UIScreen.main.bounds.size.width*0.5), y: \(UIScreen.main.bounds.size.height*0.5)")
            popoverController?.sourceRect = CGRect(x: UIScreen.main.bounds.size.width*0.5, y: UIScreen.main.bounds.size.height*0.5, width: 0, height: 0)
            popoverController?.permittedArrowDirections = []
        }
    }

    @IBAction func countryButtonClicked(_ sender: UIBarButtonItem) {
        displayCountries()
    }

    func displayCountries() {
        let alert = UIAlertController(title: "", message: "", preferredStyle: .actionSheet)

        let paragraphStyle = NSMutableParagraphStyle()
        paragraphStyle.alignment = .center

        let titleAttributedText = NSMutableAttributedString(
            string: "Select Country",
            attributes: [
                NSAttributedString.Key.paragraphStyle: paragraphStyle
            ]
        )
        alert.setValue(titleAttributedText, forKey: "attributedMessage")
        alert.addAction(UIAlertAction(title: "Cancel", style: UIAlertAction.Style.cancel, handler: countryHandler))

        for list in Countries {
            alert.addAction(UIAlertAction(title: list, style: .default, handler: countryHandler))
        }

        // For iPad
        if let poController = alert.popoverPresentationController {
            popoverController = poController
            popoverController?.sourceView = self.view

            print("displayCountries():width: x: \(UIScreen.main.bounds.size.width*0.5), y: \(UIScreen.main.bounds.size.height*0.5)")
            popoverController?.sourceRect = CGRect(x: UIScreen.main.bounds.size.width*0.5, y: UIScreen.main.bounds.size.height*0.5, width: 0, height: 0)
            popoverController?.permittedArrowDirections = []
        }

        alert.view.addSubview(UIView())
        present(alert, animated: false, completion: nil)
    }

    @IBAction func cityButtonClicked(_ sender: Any) {
        showCities()
    }

    func showCities(){
        let alert = UIAlertController(title: "Select City", message: nil, preferredStyle: .actionSheet)

        for listItem in Cities {
            alert.addAction(UIAlertAction(title: listItem.title, style: .default, handler: cityHandler))
        }
        alert.addAction(UIAlertAction(title: "Cancel", style: UIAlertAction.Style.cancel, handler: cityHandler))

        if let popoverController = alert.popoverPresentationController {
            popoverController.sourceView = self.view
            print("showCities():width: x: \(UIScreen.main.bounds.size.width*0.5), y: \(UIScreen.main.bounds.size.height*0.5)")
            popoverController.sourceRect = CGRect(x: UIScreen.main.bounds.size.width*0.5, y: UIScreen.main.bounds.size.height*0.5, width: 0, height: 0)
            popoverController.permittedArrowDirections = []
        }

        alert.view.addSubview(UIView())
        present(alert, animated: false, completion: nil)
    }
} 

popoverController must be also assigned in second alert: popoverController必须在第二个警报中分配popoverController

if let popoverController = alert.popoverPresentationController {
    self.popoverController = popoverController
...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM