PopoverPresentation控制器不以左上箭头方向显示在屏幕中心

时间:2018-04-19 07:01:24

标签: ios iphone swift uipopovercontroller

enter image description here

我想在屏幕中央显示弹出窗口。下图显示它正在移动左侧的位。

我尝试过以下代码来显示弹出控制器。

func displayPopoverVC() {

    let popOver = popupArtistStoryboard.instantiateViewController(withIdentifier: "MeasurementPopupVC") as! MeasurementPopupVC
    popOver.modalPresentationStyle = .popover
    popOver.popoverPresentationController?.permittedArrowDirections = .up

    popOver.popoverPresentationController?.sourceView = btnTmpMeasurement
    popOver.popoverPresentationController?.sourceRect = CGRect.init(x: 0, y: 0, width: btnTmpMeasurement.frame.size.width, height: btnTmpMeasurement.frame.size.height)

    if (DeviceType.IS_IPAD_PRO || DeviceType.IS_IPAD) {
        popOver.preferredContentSize = CGSize(width: 240, height: 50.0)
    } else {
        popOver.preferredContentSize = CGSize(width: 325, height: 30.0)
    }

    popOver.delegate = self
    popOver.popoverPresentationController?.delegate = self

    popOver.arrOfMeasurementData = [String]()

    if let presentation = popOver.popoverPresentationController {
        presentation.backgroundColor = UIColor(red: 236.0/255.0, green: 236.0/255.0, blue: 236.0/255.0, alpha: 0.5)
    }

    self.present(popOver, animated: true) {}
}

如果我写错了代码,请纠正我。

1 个答案:

答案 0 :(得分:0)

用户在代码下方设置屏幕中心的弹出窗口。

popOver.popoverPresentationController?.sourceRect = CGRect.init(x: 0, y: 0, width: btnTmpMeasurement.frame.size.width, height: btnTmpMeasurement.frame.size.height)
popOver.center = CGPoint(x: UIScreen.main.bounds.maxX, y: UIScreen.main.bounds.midY)

这将在垂直和水平方向的中心设置弹出窗口。 您可以将y点硬编码定义为: -

popOver.popoverPresentationController?.sourceRect = CGRect.init(x: 0, y: 0, width: btnTmpMeasurement.frame.size.width, height: btnTmpMeasurement.frame.size.height)
popOver.center = CGPoint(x: UIScreen.main.bounds.maxX, y: 100.0)