PopoverPresentation Controller not displaying on centre of the screen with the top left arrow direction

爱⌒轻易说出口 提交于 2019-12-11 17:04:47

问题


I want to display pop up in a centre of the screen. The below image displaying it's moving the bit of left side.

I have tried below code to display popover controller.

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

Please correct me if I've written any incorrect code.


回答1:


User below code to set popover in the center of screen.

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)

This will set popover in center for both vertically and horizontally. You can define y point hardcoded like:-

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)


来源:https://stackoverflow.com/questions/49914682/popoverpresentation-controller-not-displaying-on-centre-of-the-screen-with-the-t

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!