簡體   English   中英

緊湊型(iPhone)設備上的UIPopoverPresentationControllerDelegate不會觸發adaptivePresentationStyle

[英]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標記之前添加,但不工作的人。
  • Xcode對每個警告: 實例方法...幾乎與協議'UIAdaptivePresentationControllerDelegate'的可選要求...相匹配 但是,方法簽名是100%匹配。 不知道這是否是此bug的實例,有人說Xcode 10.1中仍然存在該bug

謝謝。

委托實現的功能:

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方法。
  • Xcode提供的Instance方法...幾乎符合協議'UIAdaptivePresentationControllerDelegate'錯誤的可選要求...。

對於尋求相同功能的任何人,更正后的代碼如下。

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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM