UIReferenceLibraryViewController cannot be presented as popup (always covers full screen)

半腔热情 提交于 2019-12-13 21:59:37

问题


The following Swift 3 code (credit to Hacking With Swift) creates a UIAlertController and successfully presents it as a popup from a CGRect co-ordinate on iPad, via the UIPopoverPresentationController 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)
}

However, when trying to present a different UIViewController as a popup, UIReferenceLibraryViewController (the system's mono/bi-lingual dictionary), it covers the whole screen rather than appearing as a popup.

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

What's the problem here? Is further setup of the UIReferenceLibraryViewController required, or is it simply locked against being presented as a popup?

My target is iOS 10.2, Universal; building for iPad Pro (12.9 inch).

If there is no way to solve this, I would equally accept any answer that provides the code needed to present the UIReferenceLibraryViewController as a popover via another library (bonus points if it works as a popover on iPhone as well as iPad).


回答1:


You need to set the view controller's modalPresentationStyle to .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 does this for you since it is always a popover.



来源:https://stackoverflow.com/questions/43219587/uireferencelibraryviewcontroller-cannot-be-presented-as-popup-always-covers-ful

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!