UIPopoverPresentationController将popover显示为全屏

The*_*mad 39 popover ios uipopover swift

我试图用来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)
    }
}
Run Code Online (Sandbox Code Playgroud)

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

func adaptivePresentationStyle(for controller: UIPresentationController, traitCollection: UITraitCollection) -> UIModalPresentationStyle {
    return .none
}
Run Code Online (Sandbox Code Playgroud)

gab*_*ler 38

在iPhone中,您应该添加以下内容以呈现弹出窗口.

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

  • 在Swift 4上,这不会给出错误,但是不会解决问题.轻微的签名调整:`func adaptivePresentationStyle(对于控制器:UIPresentationController) - > UIModalPresentationStyle {return UIModalPresentationStyle.none}` (7认同)
  • 你在哪里添加这个?到 Popover 的视图控制器? (2认同)

小智 18

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

func adaptivePresentationStyle(for controller: UIPresentationController, traitCollection: UITraitCollection) -> UIModalPresentationStyle
{
    return .none
}
Run Code Online (Sandbox Code Playgroud)

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


mou*_*igo 5

对于Swift3 + / IOS10 +,在处理iPhone时:

您必须在以下位置添加UIPopoverPresentationControllerDelegate委托:

class YourClass:  UIViewController, UIPopoverPresentationControllerDelegate { ...
Run Code Online (Sandbox Code Playgroud)

然后,在同一父类(将显示弹出窗口)中实现以下方法。

func adaptivePresentationStyle(for controller: UIPresentationController, traitCollection: UITraitCollection) -> UIModalPresentationStyle
{
    return .none
}
Run Code Online (Sandbox Code Playgroud)

然后在下面设置弹出窗口配置:

myPopover.modalPresentationStyle = .popover
myPopover.popoverPresentationController?.sourceRect = VIEWTOPOINTTHEARROW.frame
myPopover.popoverPresentationController?.sourceView = self.view
myPopover.popoverPresentationController?.delegate = self
Run Code Online (Sandbox Code Playgroud)

您也可以为popover类设置一些配置

 class MyPopover: UIViewController {

 override func viewDidLoad() {
    super.viewDidLoad()
    //popover size
    self.preferredContentSize = CGSize(width: 320, height: 200) 
    //sets the arrow of the popover to same color of background
    self.popoverPresentationController?.backgroundColor = self.view.backgroundColor
   }

 }
Run Code Online (Sandbox Code Playgroud)