问题
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