Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/94.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios Xcode,Swift:如何将弹出框居中_Ios_Xcode_Swift_Popover - Fatal编程技术网

Ios Xcode,Swift:如何将弹出框居中

Ios Xcode,Swift:如何将弹出框居中,ios,xcode,swift,popover,Ios,Xcode,Swift,Popover,通过文件链接到我的github帐户,只需下载zip: 当我点击“popover”链接时,我想把popover放在屏幕中央 我尝试引用一些stackoverflow问题,例如: 但是没有运气。我是swift中的一个noob,我只使用swift,而不是objective C 我看到的屏幕截图: 您可以使用下面的代码在视图中居中放置UIPopover let controller = vc.popoverPresentationController controller?.permittedArro

通过文件链接到我的github帐户,只需下载zip:

当我点击“popover”链接时,我想把popover放在屏幕中央

我尝试引用一些stackoverflow问题,例如:

但是没有运气。我是swift中的一个noob,我只使用swift,而不是objective C

我看到的屏幕截图:

您可以使用下面的代码在视图中居中放置
UIPopover

let controller = vc.popoverPresentationController
controller?.permittedArrowDirections = UIPopoverArrowDirection(rawValue: 0)
controller?.sourceView = self.view
controller?.sourceRect = CGRectMake(0.0, self.view.layer.bounds.height * 0.5,0.0,0.0)
vc.preferredContentSize=CGSize(width: 400, height: 200)
替换

controller?.sourceRect = CGRectMake(0.0, self.view.layer.bounds.height * 0.5,0.0,0.0)

将popover内容水平和垂直居中。

Swift 3:

override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
    if let popoverPresentationController = segue.destination.popoverPresentationController {
        let controller = popoverPresentationController
        controller.permittedArrowDirections = UIPopoverArrowDirection(rawValue: 0)
        controller.sourceView = self.view
        controller.sourceRect = CGRect(x: UIScreen.main.bounds.width * 0.5 - 200, y: UIScreen.main.bounds.height * 0.5 - 100, width: 400, height: 200)
        segue.destination.preferredContentSize=CGSize(width: 400, height: 200)
    }
}

谢谢你,问题是,这似乎只有在弹出框的大小与我设置的一样时才起作用,但是如果我将最后一行设置为:vc.preferredContentSize=CGSize(宽度:300,高度:100),它垂直居中,而不是水平居中,你知道如何解决这个问题吗?
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
    if let popoverPresentationController = segue.destination.popoverPresentationController {
        let controller = popoverPresentationController
        controller.permittedArrowDirections = UIPopoverArrowDirection(rawValue: 0)
        controller.sourceView = self.view
        controller.sourceRect = CGRect(x: UIScreen.main.bounds.width * 0.5 - 200, y: UIScreen.main.bounds.height * 0.5 - 100, width: 400, height: 200)
        segue.destination.preferredContentSize=CGSize(width: 400, height: 200)
    }
}