UIPopoverPresentationController笔

2020-07-03  本文已影响0人  VervertomJC

练习微博下拉弹窗,想起来 UIPopoverPresentationController这个控制器可以实现,弹窗,只是弹窗动画还不知道需要怎么写,先记录下使用方法:
外貌协会,先看图:


popover.png

首先可以先参考苹果官方的教程 Presenting a View Controller in a Popover.

import UIKit

class ViewController: UIViewController {

    @IBOutlet weak var popoverBtn: UIButton!
    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view.
        
    }

    @IBAction func popoverAction(_ sender: UIButton) {
        ///在弹窗中呈现一个 视图控制器
        let secVC = SecondViewController()
        //1.设置模态呈现的风格  UIModalPresentationPopover
        secVC.modalPresentationStyle = .popover
        //2.设置视图控制器 首选内容大小
        secVC.preferredContentSize = CGSize(width: 100, height: 150)
        
        /*
         使用关联的 UIPopoverPresentationController 对象设置弹窗锚点,可通过视图控制器popoverPresentationController属性
         进行访问
         
         方式:方式1: 设置sourceView和sourceRect
              方式2: 设置 barButtonItem属性
         */
        secVC.popoverPresentationController?.sourceView = popoverBtn
        secVC.popoverPresentationController?.sourceRect = popoverBtn.bounds
//        secVC.popoverPresentationController?.barButtonItem = self.navigationItem.rightBarButtonItem
        //设置箭头指向
        secVC.popoverPresentationController?.permittedArrowDirections = .up
        secVC.popoverPresentationController?.delegate = self
        present(secVC, animated: true, completion: nil)
    }

}

//遵循代理实现代理方法
extension ViewController: UIPopoverPresentationControllerDelegate {
    //    func adaptivePresentationStyle(for controller: UIPresentationController) -> UIModalPresentationStyle {
    //        return .none
    //    }
    
    //iOS 8.3后使用这个方法来处理 特征改变
    //- NOTE: 不实现弹窗将无法显示
    func adaptivePresentationStyle(for controller: UIPresentationController, traitCollection: UITraitCollection) -> UIModalPresentationStyle {
        return .none
    }

    func popoverPresentationController(_ popoverPresentationController: UIPopoverPresentationController, willRepositionPopoverTo rect: UnsafeMutablePointer<CGRect>, in view: AutoreleasingUnsafeMutablePointer<UIView>) {
        
    }
}
上一篇下一篇

猜你喜欢

热点阅读