UIReferenceLibraryViewController 无法显示为弹出窗口(始终覆盖全屏)
UIReferenceLibraryViewController cannot be presented as popup (always covers full screen)
以下Swift 3 代码(贷记给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(系统的 mono/bi-lingual 字典)会覆盖整个屏幕,而不是显示为弹出窗口。
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).
您需要将视图控制器的 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
为您完成此操作,因为它始终是弹出窗口。
以下Swift 3 代码(贷记给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(系统的 mono/bi-lingual 字典)会覆盖整个屏幕,而不是显示为弹出窗口。
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).
您需要将视图控制器的 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
为您完成此操作,因为它始终是弹出窗口。