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

标签 ios swift ipad uiviewcontroller popup

以下 Swift 3 代码(归功于 Hacking With Swift)创建了一个 UIAlertController 并通过UIPopoverPresentationController应用程序接口(interface):

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)。

最佳答案

您需要将 View Controller 的 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 为您做这件事,因为它始终是一个弹出窗口。

关于ios - UIReferenceLibraryViewController 无法显示为弹出窗口(始终覆盖全屏),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43219587/

相关文章:

swift - 如何处理 serviceSubscriberCellularProviders 以仅从物理 SIM 卡而不是 eSIM 获取数据?

javascript - 在 iOS Safari 中调试 Google Maps API 堆栈溢出

ios - TableView UIImage 渲染问题

ios - 为什么 JavaPNS 将生产推送消息发送到沙箱服务器?

ios - SwiftUI - 滚动时关闭键盘

ios - UITableViewController 表删除后不会刷新,但会在 viewWillAppear 上刷新

ios - 将数组从 1 个 View Controller 发送到 2 个其他单独的 View Controller

swift - 如何确定 Metal 设备 VR 支持

iphone - 如何修复 10 分钟后锁定和解锁屏幕时发生的崩溃?

ios - 使用 evaluateJavascript 方法在 WKWebview 上设置本地镜像