まずはpopさせるクラスを用意する
class PopViewController:UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
view.backgroundColor = .systemOrange
}
}
遷移前の画面でコード作成。
//遷移先のVCをインスタンス化
let viewController = PopViewController() //popoverで表示するViewController
//遷移方法をpopover
viewController.modalPresentationStyle = .popover
//吹き出しのサイズを設定する
viewController.preferredContentSize = CGSize(width: 200, height: 150)
let presentationController = viewController.popoverPresentationController
//up 吹き出しを表示させる向きを設定する
presentationController?.permittedArrowDirections = .up
//どこを基準に表示させるかのsourceViewを設定
presentationController?.sourceView = cell
//吹き出しを表示させる位置を設定。
presentationController?.sourceRect = cell.bounds
//popoverさせるためのdelegateメソッド
viewController.presentationController?.delegate = self
present(viewController, animated: true, completion: nil)
必要なdelegateメソッドを用意
class ViewController: UIViewController, UIPopoverPresentationControllerDelegate{
//Iphoneで表示させる際のdelegateMethod
func adaptivePresentationStyle(for controller: UIPresentationController, traitCollection: UITraitCollection) -> UIModalPresentationStyle {
return .none
}
}
どこかで関数を発火すればOK!!