问题
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