【问题标题】:Popoverview is not centred on iPad rotationPopoverview 不以 iPad 旋转为中心
【发布时间】:2019-09-02 17:37:49
【问题描述】:

在 iPad 旋转时,iPad 上的以下代码弹出框未居中以显示第二个警报(城市)。不过,它适用于第一个警报(国家)。我已经打印了这些值,两个警报都是一样的。似乎尽管坐标值正确,但它并未在设备旋转中心显示第二次警报。

为什么它没有显示在第二个警报的中心?如何解决?

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)
    }
} 

【问题讨论】:

    标签: ios swift uipopovercontroller uipopoverpresentationcontroller


    【解决方案1】:

    popoverController 也必须在第二个警报中分配:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2012-10-08
      • 2018-04-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-06-24
      相关资源
      最近更新 更多