how to use popover controller in iPhone

后端 未结 1 1004
生来不讨喜
生来不讨喜 2021-01-02 22:08

I couldn\'t show popover controller as popover in iPhone whereas it works very well with iPad.

Any ideas on how to do that in iPhone.

As far as I have search

相关标签:
1条回答
  • 2021-01-02 22:25

    Set yourself as the popover view controller's delegate before presenting it, and implement the delegate method adaptivePresentationStyle(for:traitCollection:) to return .none. This will cause the popover to stop adapting on iPhone as a fullscreen presented view controller and turn into an actual popover just like on the iPad.

    This is a complete working example that presents the popover in response to a button tap:

    class ViewController: UIViewController {
        @IBAction func doButton(_ sender: Any) {
            let vc = MyPopoverViewController()
            vc.preferredContentSize = CGSize(400,500)
            vc.modalPresentationStyle = .popover
            if let pres = vc.presentationController {
                pres.delegate = self
            }
            self.present(vc, animated: true)
            if let pop = vc.popoverPresentationController {
                pop.sourceView = (sender as! UIView)
                pop.sourceRect = (sender as! UIView).bounds
            }
        }
    }
    extension ViewController : UIPopoverPresentationControllerDelegate {
        func adaptivePresentationStyle(for controller: UIPresentationController, traitCollection: UITraitCollection) -> UIModalPresentationStyle {
            return .none
        }
    }
    
    0 讨论(0)
提交回复
热议问题