0

enter image description here

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.

Gautam Sareriya
  • 1,833
  • 19
  • 30
  • 1
    Possible duplicate of [how to center a popoverview in swift](https://stackoverflow.com/questions/31759615/how-to-center-a-popoverview-in-swift) – Prashant Tukadiya Apr 19 '18 at 07:03
  • @PrashantTukadiya, it's a different scenario than posted question. I want to arrow in left side and pop up view in centre of SourceView. – Gautam Sareriya Apr 19 '18 at 07:32

1 Answers1

0

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)
Manish Mahajan
  • 2,062
  • 1
  • 13
  • 19