![](/img/trans.png)
[英]Difference between 2 adaptivePresentationStyle methods of the UIPopoverPresentationControllerDelegate protocol
[英]adaptivePresentationStyle not Triggering for UIPopoverPresentationControllerDelegate on Compact (iPhone) Devices
我正在嘗試顯示一個UIViewController
作為另一個的彈出窗口。 為此,我建立了以下...
func showPopover(ofViewController popoverViewController: UIViewController, sender: UIView) {
popoverViewController.modalPresentationStyle = .popover
popoverViewController.popoverPresentationController?.sourceView = sender
popoverViewController.popoverPresentationController?.sourceRect = sender.bounds
popoverViewController.popoverPresentationController?.delegate = self
self.present(popoverViewController, animated: true, completion: nil)
}
但是,新的VC始終在緊湊型設備上以全屏模式顯示,而不是實際的彈出窗口。 根據我在此處和此處閱讀的內容,這是正常的行為,但應通過委托進行自定義。
我已經聲明要呈現的VC為實現UIPopoverPresentationControllerDelegate
,將其設置為委托,並實現所需的方法; 但是,永遠不會調用委托方法。 這意味着無論如何,“彈出窗口”仍會以模態顯示。
任何的建議都受歡迎。
其他一些標注:
viewControllerForAdaptivePresentationStyle
做如果被調用@objc
標記之前添加,但不工作的人。 謝謝。
委托實現的功能:
func adaptivePresentationStyle(for: UIPresentationController) -> UIModalPresentationStyle {
return UIModalPresentationStyle.popover
}
func adaptivePresentationStyle(for controller: UIPresentationController, traitCollection: UITraitCollection) -> UIModalPresentationStyle {
return UIModalPresentationStyle.popover
}
func presentationController(_ controller: UIPresentationController, viewControllerForAdaptivePresentationStyle style: UIModalPresentationStyle) -> UIViewController? {
switch style {
case .fullScreen: // Configuration for full-screen
default: return controller.presentedViewController
}
}
感謝Paulw11確認問題是由在UIViewController
擴展中實現代碼引起的。 這導致了奇怪的行為,從而使代碼沒有像往常一樣被調用。
當遷移到UIViewController
的共享子類時,以下問題已全部解決:
adaptivePresentationStyle
方法永遠不會被調用。 @objc
標記之后才調用viewControllerForAdaptivePresentationStyle
方法。 對於尋求相同功能的任何人,更正后的代碼如下。
class CustomViewController: UIViewController {
func showPopover(ofViewController popoverViewController: UIViewController, originView: UIView) {
popoverViewController.modalPresentationStyle = UIModalPresentationStyle.popover
if let popoverController = popoverViewController.popoverPresentationController {
popoverController.delegate = self
popoverController.sourceView = originView
popoverController.sourceRect = originView.bounds
popoverController.backgroundColor = popoverViewController.view.backgroundColor
popoverController.permittedArrowDirections = UIPopoverArrowDirection.any
}
self.present(popoverViewController, animated: true)
}
}
extension CustomViewController: UIPopoverPresentationControllerDelegate {
func adaptivePresentationStyle(for: UIPresentationController) -> UIModalPresentationStyle {
return UIModalPresentationStyle.none
//return UIModalPresentationStyle.fullScreen
}
func adaptivePresentationStyle(for controller: UIPresentationController, traitCollection: UITraitCollection) -> UIModalPresentationStyle {
if traitCollection.horizontalSizeClass == .compact {
return UIModalPresentationStyle.none
//return UIModalPresentationStyle.fullScreen
}
//return UIModalPresentationStyle.fullScreen
return UIModalPresentationStyle.none
}
func presentationController(_ controller: UIPresentationController, viewControllerForAdaptivePresentationStyle style: UIModalPresentationStyle) -> UIViewController? {
switch style {
case .fullScreen: // Configuration for full-screen
default:
return controller.presentedViewController
}
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.