我有以下代码来显示没有箭头的 popoverview(对话框),效果很好。唯一的问题是,对话框显示在左上角 (iPad)。我想将 View 置于屏幕中心。
要在我的以下代码中更改或添加什么? :
func show_help(){
let storyboard = UIStoryboard(name: "Main", bundle: nil)
let controller = storyboard.instantiateViewControllerWithIdentifier("Help") as! UIViewController
controller.modalPresentationStyle = UIModalPresentationStyle.popover
let popoverPresentationController = controller.popoverPresentationController
// result is an optional (but should not be nil if modalPresentationStyle is popover)
if let _popoverPresentationController = popoverPresentationController {
// set the view from which to pop up
_popoverPresentationController.sourceView = self.view;
_popoverPresentationController.permittedArrowDirections = UIPopoverArrowDirection.allZeros;
// present (id iPhone it is a modal automatic full screen)
self.presentViewController(controller, animated: true, completion: nil)
}
}
附加信息
在我的 View 中,它链接到我的 View Controller ,我设置了这样的首选大小:
override func viewDidLoad() {
let dialogheigth:CGFloat = self.view.frame.height * 0.5;
let dialogwidth:CGFloat = self.view.frame.width * 0.5;
self.preferredContentSize = CGSizeMake(dialogwidth,dialogheigth);
}
最佳答案
您需要为弹出窗口提供源矩形。
来自 Apple 文档:源矩形是指定 View 中用于锚定弹出框的矩形。将此属性与 sourceView 属性结合使用以指定弹出框的 anchor 位置。
在你的情况下,在
_popoverPresentationController.sourceView = self.view;
添加:
_popoverPresentationController.sourceRect = CGRectMake(CGRectGetMidX(self.view.bounds), CGRectGetMidY(self.view.bounds),0,0)
它会成功的!
关于ios - 如何在 swift 中将 popoverview 居中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31759615/