使用以编程方式创建的UIPopoverPresentationController强制iPhone上的popover

时间:2014-12-29 07:13:22

标签: ios swift

我需要创建一个新的弹出窗口视图,因为在编译期间,Interface Builder中的锚点不可见。

根据this post,实施委托方法将强制在iPhone上使用Popover。 (由于我不明白的原因)

在帖子中显示的segue上效果很好。但我无法以非分段的方式使用它。 (Popover确实出现在iPad上)

请帮忙!

代码如下:

func showOptions() {
    let contentView = UIStoryboard(name: "Main", bundle: nil).instantiateViewControllerWithIdentifier("PasteOption") as NewPasteOptionViewController
    contentView.modalPresentationStyle = UIModalPresentationStyle.Popover
    contentView.preferredContentSize = CGSizeMake(200.0, 200.0)
    presentViewController(contentView, animated: true, completion: nil)
    var _popoverPresentationController = contentView.popoverPresentationController!
    _popoverPresentationController.delegate = self
    _popoverPresentationController.permittedArrowDirections = UIPopoverArrowDirection.Any
    _popoverPresentationController.sourceView = view
    _popoverPresentationController.sourceRect = self.navigationItem.titleView!.frame
}
// this function below is never called.
func adaptivePresentationStyleForPresentationController(controller: UIPresentationController) -> UIModalPresentationStyle {
    return UIModalPresentationStyle.None
}

附加说明:

呈现视图控制器在另一个视图中显示为模态表单,并封装在导航控制器中。 呈现的视图控制器是自定义表视图控制器。

2 个答案:

答案 0 :(得分:9)

似乎将行presentViewController(contentView, animated: true, completion: nil) 移动到函数的末尾将解决问题。

答案 1 :(得分:1)

let contentView  = 
PLMainNavigationManager.sharedInstance.storyboard.instantiateViewControllerWithIdentifier("PLSearchVCID") as! PLSearchVC
        contentView.modalPresentationStyle = UIModalPresentationStyle.Popover
        contentView.preferredContentSize = CGSizeMake(400.0, 500.0)
        var _popoverPresentationController = contentView.popoverPresentationController!
        _popoverPresentationController.delegate = self
        _popoverPresentationController.permittedArrowDirections = UIPopoverArrowDirection.Any
        _popoverPresentationController.sourceView = self.view
       _popoverPresentationController.sourceRect = CGRectMake(-30, -280, 320, 400)  
        PLMainNavigationManager.sharedInstance.navigationController?.presentViewController(contentView, animated: true, completion: nil)
相关问题