UIPopoverPresentationController以全屏显示popup窗口

我正在尝试使用UIPopoverPresentationController来显示不占用整个屏幕的popover窗口。 我跟着很多不同的教程,没有运气。

这是我的代码。 它正确地实例化ViewController ,但它占用了整个屏幕,而不是像我在preferredContentSize定义的那样只是一个较小的屏幕。

 func showPopover() { let vc = self.storyboard?.instantiateViewControllerWithIdentifier("PopupTimePickerViewController") as PopupTimePickerViewController vc.modalPresentationStyle = .Popover vc.preferredContentSize = CGSizeMake(200, 100) if let presentationController = vc.popoverPresentationController { presentationController.delegate = self presentationController.permittedArrowDirections = .Up presentationController.sourceView = self.view presentationController.sourceRect = CGRectMake(0, 0, 50, 50) self.presentViewController(vc, animated: true, completion: nil) } } 

更新9/27/16正确的答案

 func adaptivePresentationStyle(for controller: UIPresentationController, traitCollection: UITraitCollection) -> UIModalPresentationStyle { return .none } 

在iPhone中,您应该添加以下内容以显示popup窗口。

 func adaptivePresentationStyleForPresentationController(controller: UIPresentationController!) -> UIModalPresentationStyle { // Return no adaptive presentation style, use default presentation behaviour return .None } 

对于Swift3 / IOS10,看起来像我们需要做一些类似的事情

 func adaptivePresentationStyle(for controller: UIPresentationController, traitCollection: UITraitCollection) -> UIModalPresentationStyle { return .none } 

添加这个答案,以防有人遇到这个问题,就像我在迁移到swift3 / IOS10时一样

接受的答案是正确的。 有关完整性,请参阅在Apple文档中将呈现的视图控制器修改为新样式 :

使用委托的adaptivePresentationStyleForPresentationController:方法指定与默认不同的演示文稿样式。 转换到紧凑型环境时,唯一受支持的样式是两个全屏样式或UIModalPresentationNone 。 返回UIModalPresentationNone通知演示控制器忽略紧凑环境,并继续使用先前的演示文稿样式。 在popup窗口的情况下,忽略更改会为所有设备提供与iPad类似的popup窗口行为。

确保满足在popup窗口中呈现View Controller所需的configuration:

在将[呈现的视图控制器]的模式performance风格设置为UIModalPresentationPopover ,configuration以下与UIModalPresentationPopover相关的属性:

  • 将视图控制器的preferredContentSize属性设置为所需的大小。
  • 使用可从视图控制器的popoverPresentationController属性访问的关联UIPopoverPresentationController对象设置popup式定位点。
  • 只设置下列之一:
    • barButtonItem属性设置为一个栏button项。
    • sourceViewsourceRect属性设置为您的某个视图中的特定区域。