问题描述
以下Swift 3代码(贷记到使用Swift破解)创建一个UIAlertController
,并通过 UIPopoverPresentationController API:
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)
}
但是,当尝试显示其他UIViewController
作为弹出窗口时, UIReferenceLibraryViewController (系统的单/双语言词典),它覆盖了整个屏幕,而不是显示为弹出窗口.
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)
}
这是什么问题?是否需要进一步设置UIReferenceLibraryViewController
,还是只是将其锁定为不显示为弹出窗口?
What's the problem here? Is further setup of the UIReferenceLibraryViewController
required, or is it simply locked against being presented as a popup?
我的目标是iOS 10.2(通用); iPad Pro(12.9英寸)的外壳.
My target is iOS 10.2, Universal; building for iPad Pro (12.9 inch).
如果没有办法解决此问题,我将同样接受任何提供通过另一个库将UIReferenceLibraryViewController
作为弹出窗口呈现的代码的答案(如果在iPhone和iPad上作为弹出窗口使用,则需要加分) ).
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).
推荐答案
您需要将视图控制器的modalPresentationStyle
设置为.popover
.
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
为您执行此操作,因为它始终是弹出窗口.
UIAlertController
does this for you since it is always a popover.
这篇关于UIReferenceLibraryViewController无法显示为弹出窗口(始终覆盖全屏)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!