iOS PopoverController

谭研
2023-12-01

iOS PopoverController

背景

  • UIPopoverController

在iOS9.0 已废弃

UIKIT_EXTERN API_DEPRECATED("UIPopoverController is deprecated. Popovers are now implemented as UIViewController presentations. Use a modal presentation style of UIModalPresentationPopover and UIPopoverPresentationController.", ios(3.2, 9.0)) NS_SWIFT_UI_ACTOR

  • UIPopoverPresentationController

iOS 8 之后用来取代 UIPopoverController

UIPopoverPresentationController

    @IBAction func buttonClick(_ sender: UIButton) {
        
        let popover = UIViewController()
        popover.view.backgroundColor = .purple
        popover.modalPresentationStyle = .popover
        popover.popoverPresentationController?.sourceView = sender
        popover.popoverPresentationController?.sourceRect = CGRect(x: sender.frame.size.width / 2, y: sender.frame.size.height, width: 0, height: 0)
        popover.popoverPresentationController?.permittedArrowDirections = .any
        popover.popoverPresentationController?.delegate = self
        self.present(popover, animated: true)
    }

上面的代码在 iPad上执行会模态出一个 popver 弹窗试图,在 iPhone 上不会有 popver 试图,而是一个弹出的满屏的试图。

如果需要在iPhone 上也弹出 popver 试图,需要实现如下代理方法:

    func adaptivePresentationStyle(for controller: UIPresentationController) -> UIModalPresentationStyle {
        return .none
    }

此外,我们无需创建 UIPopoverPresentationController 的对象,系统提供的 API,将 popoverPresentationController 作为 UIViewController 的一个属性,我们只需要直接获取使用即可。

系统提供的API如下:

extension UIViewController {

    
    @available(iOS 8.0, *)
    open var presentationController: UIPresentationController? { get }

    @available(iOS 15.0, *)
    open var sheetPresentationController: UISheetPresentationController? { get }

    @available(iOS 8.0, *)
    open var popoverPresentationController: UIPopoverPresentationController? { get }

    
    // Gets the presentation controller managing this view controller. If the original presentation controller has adapted, this returns the adaptive presentation controller. If this view controller has not yet been presented, this property returns nil.
    @available(iOS 16.0, *)
    open var activePresentationController: UIPresentationController? { get }

    
    // modalInPresentation is set on the view controller when you wish to force the presentation hosting the view controller into modal behavior. When this is active, the presentation will prevent interactive dismiss and ignore events outside of the presented view controller's bounds until this is set to NO.
    @available(iOS 13.0, *)
    open var isModalInPresentation: Bool
}
 类似资料:

相关阅读

相关文章

相关问答