UIReferenceLibraryViewController无法显示为弹出窗口(始终覆盖全屏)

时间:2017-04-04 23:48:30

标签: ios swift ipad uiviewcontroller popup

以下Swift 3代码(归功于Hacking With Swift)会创建一个UIAlertController,并通过{{3}成功将其显示为来自iPad CGRect坐标的弹出窗口API:

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作为弹出式广告UIPopoverPresentationController(系统的单/双语词典)时,它会覆盖整个屏幕而不是显示为弹出窗口。

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,Universal;适用于iPad Pro(12.9英寸)的建筑。

如果没有办法解决这个问题,我同样会接受任何答案,提供将UIReferenceLibraryViewController作为弹出窗口通过另一个图书馆提供所需的代码(如果它作为iPhone上的弹出窗口也可以获得奖励积分作为iPad)。

1 个答案:

答案 0 :(得分:2)

您需要将视图控制器的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为你做这件事,因为它总是一个弹出窗口。