【问题标题】:UIReferenceLibraryViewController cannot be presented as popup (always covers full screen)UIReferenceLibraryViewController 不能显示为弹出窗口(总是覆盖全屏)
【发布时间】:2017-08-30 09:11:42
【问题描述】:

以下 Swift 3 代码(归功于 Hacking With Swift)创建了一个 UIAlertController,并通过 UIPopoverPresentationController API 在 iPad 上以 CGRect 坐标的弹出窗口成功呈现它:

func popUpToSelectTheLanguage(){
    let ac = UIAlertController(title: "Set expected language...", message: nil, preferredStyle: .actionSheet)
    ac.addAction(UIAlertAction(title: "English", style: .default, handler: { (action) in /* Action to take */ }))
    ac.addAction(UIAlertAction(title: "French", style: .default, handler: { (action) in /* Action to take */ }))
    ac.addAction(UIAlertAction(title: "Cancel", style: .cancel))

    let popover = ac.popoverPresentationController
    popover?.sourceView = view
    popover?.sourceRect = CGRect(x: 32, y: 32, width: 64, height: 64)
    present(ac, animated: true)
}

但是,当尝试将不同的UIViewController 显示为弹出窗口时,UIReferenceLibraryViewController(系统的单语/双语词典)会覆盖整个屏幕,而不是作为弹出窗口出现。

func popUpTheDictionary(){
    let dic = UIReferenceLibraryViewController(term: "hi" as String)

    let popover = dic.popoverPresentationController
    popover?.sourceView = view
    popover?.sourceRect = CGRect(x: 32, y: 32, width: 64, height: 64)
    present(dic, animated: true)
}

这里有什么问题?是否需要进一步设置 UIReferenceLibraryViewController,或者它只是被锁定以防止显示为弹出窗口?

我的目标是 iOS 10.2,通用;为 iPad Pro(12.9 英寸)构建。

如果没有办法解决这个问题,我同样会接受任何提供通过另一个库将UIReferenceLibraryViewController 呈现为弹出窗口所需的代码的答案(如果它在 iPhone 和 iPad 上作为弹出窗口工作,则加分)。

【问题讨论】:

    标签: ios swift ipad uiviewcontroller popup


    【解决方案1】:

    您需要将视图控制器的modalPresentationStyle 设置为.popover

    func popUpTheDictionary(){
        let dic = UIReferenceLibraryViewController(term: "hi" as String)
        dic.modalPresentationStyle = .popover // add this
    
        let popover = dic.popoverPresentationController
        popover?.sourceView = view
        popover?.sourceRect = CGRect(x: 32, y: 32, width: 64, height: 64)
        present(dic, animated: true)
    }
    

    UIAlertController 为您执行此操作,因为它始终是一个弹出窗口。

    【讨论】:

    • 当我添加您建议的行时,它会引发 NSGenericException:'UIPopoverPresentationController (<UIPopoverPresentationController: 0x7fe4124a6700>) should have a non-nil sourceView or barButtonItem set before the presentation occurs.'。似乎在打开弹出框以设置 sourceView 时遇到问题。这可能与Configuring a Popover for Display 中提到的竞态条件有关。似乎找不到解决办法;有什么想法吗?
    • 注意:我已尝试将 present() 调用移至弹出框配置之前,正如我在上面链接的文档中所建议的那样,但没有成功。此外,当我尝试强制展开弹出框时,它会立即失败,因此可能会提示存在一些问题。
    • 在访问dic.popoverPresentationController之前尝试访问dicview属性。
    • 我刚刚尝试在声明 dic 的行之后立即添加行 let hailmary = dic.view。它没有任何区别(也没有与present() 处于任何可能的位置);每次都有相同的错误信息。这是您访问view 属性时的想法吗?
    • 好的,看起来问题只是我盲目地将您建议的行 dic.modalPresentationStyle = .popover 放在实例化弹出框的行下,而不是实例化 UIPopoverPresentationController 的行下。对不起红鲱鱼。似乎现在正在工作;非常激动!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-01-05
    • 1970-01-01
    • 2015-10-01
    • 2014-06-29
    • 1970-01-01
    • 1970-01-01
    • 2016-10-29
    相关资源
    最近更新 更多