【问题标题】:Force popover on iPhone with programmatically created UIPopoverPresentationController使用以编程方式创建的 UIPopoverPresentationController 在 iPhone 上强制弹出窗口
【发布时间】:2014-12-29 07:13:21
【问题描述】:

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

根据this post,实现委托方法将强制 iPhone 上的 Popover。 (因为我不明白的原因)

如帖子所示,在 segue 上效果很好。但我不能让它以非 segue 的方式工作。 (弹出窗口确实出现在 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
}

补充说明:

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

【问题讨论】:

    标签: ios swift


    【解决方案1】:

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

    【讨论】:

    • 你是对的。奇怪的是文档确实将该语句放在首位(尽管那段特定的代码没有设置委托)
    • Delegate设置应该放在presentViewController之前,你怎么看?
    • 如果您在演示后设置演示控制器,您希望演示如何成功?
    • @LeoNatan,你是什么意思,修复建议在演示之前设置演示控制器。
    • 答案效果很好。苹果的文档似乎已经过时了。正如它所说,“使用弹出框样式呈现视图控制器会创建一个弹出框呈现控制器来管理呈现过程。” “在调用 presentViewController:animated:completion: 之后配置 popover 演示控制器可能看起来有悖常理,但 UIKit 在您启动演示之前不会创建演示控制器。”
    【解决方案2】:
    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)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2019-06-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-01-17
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多