ios - 如何在 swift 中将 popoverview 居中

标签 ios swift modal-dialog uipopovercontroller

我有以下代码来显示没有箭头的 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)
    }

}

附加信息

enter image description here

在我的 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)

它会成功的!

enter image description here

关于ios - 如何在 swift 中将 popoverview 居中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31759615/

相关文章:

javascript - Angular JS 应用程序设备后退按钮必须有确认对话框

ios - 为扩展 View 添加委托(delegate)和数据源

ios - Swift UITextField 文本通过 TableViewCell 中的按钮操作清除

ios - Swift:迭代字典项并将其与数组中的字典项进行比较

ios - 将事件传递给 UITableView 之外的 UITableViewCell

c# - 如何使用模态窗口将 WM_INPUTLANGCHANGEREQUEST 发送到应用程序?

ios - 无法请求访问 Swift 4.2 中的 IOS 日历的权限

ios - Sprite Kit,灰色背景而不是图像

ios - 如何在 UITableViews 中编辑多个部分的标题?

jquery - 淡入后隐藏模态不居中