12

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 searched I couldn't find any.

anyways to make the popover appear in iphone as it is in iPad is appreciated !

Shankar Naik
  • 401
  • 5
  • 16
  • 1
    Possible duplicate of [popover doesn't display on iphone](https://stackoverflow.com/questions/41407782/popover-doesnt-display-on-iphone) – Warren Burton May 19 '18 at 17:14
  • This doesn't solve my problem – Shankar Naik May 19 '18 at 17:26
  • "Generally, popovers should be reserved for use in iPad apps. In iPhone apps, utilize all available screen space by presenting information in a full-screen modal view, rather than in a popover. For related guidance, see Modality." – Ning Jan 06 '20 at 19:10

1 Answers1

41

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
    }
}
matt
  • 515,959
  • 87
  • 875
  • 1,141
  • For code supporting both iPhone and iPad may I suggest: ```func adaptivePresentationStyle(for controller: UIPresentationController, traitCollection: UITraitCollection) -> UIModalPresentationStyle { return UIDevice.current.userInterfaceIdiom == .pad ? .popover : .none }``` – David H Oct 05 '22 at 13:03
  • 1
    @DavidH You don't understand the code. If we're on an iPad plain and simple, this method won't even be called, and the popover will be a popover. – matt Oct 05 '22 at 13:27
  • @DavidH But what happened when you _tried_ my code on the iPad? – matt Oct 05 '22 at 22:41
  • Matt, so of course it worked as expected on iPhone. But that delegate method is called on iPad - I tested it just now, however it still works as before on iPad. – David H Oct 06 '22 at 14:26