0

I know that the normal behavior on iPad is to show a popover view while on iPhone it switches to a full screen, but I don't want the full screen on the smaller devices. Is there a way to prevent this? In UIKit we could override the adaptivepresentationstyle func like this(from https://stackoverflow.com/a/50428131/412154)

 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
    }
}

wondering if anyone found something similar to override for swiftui

thanks for the help!

Prasanth
  • 473
  • 4
  • 19
  • In SwiftUI there mostly nothing to override - it is struct-based, so just wrap your `ViewController` with `adaptivePresentationStyle` into `UIViewControllerRepresentative` – Asperi May 25 '20 at 12:29
  • Ok I was hoping somehow it would obey the horizontal and vertical size class setting. – Prasanth May 25 '20 at 18:50
  • I've seen a lot of people mention how to use uiviewcontrollerrepresentative. I have some of the code working but wondering how to pass in the correct information for button location to anchor the view and show the pointing arrow at the calling button. Does anyone have a working example of this? – Prasanth May 27 '20 at 04:48

1 Answers1

0

Here is another solution that subclasses uihostingcontroller but I don’t know how I would incorporate it into my swiftui views since I’m trying to achieve this for some deeper views

sampleproject

Heres and alternative that worked better for me

resizable popover

Prasanth
  • 473
  • 4
  • 19
  • 1
    The solution in the second link saved my day, and I improved it. https://stackoverflow.com/a/69365237/13769980 – Kou Ariga Sep 28 '21 at 16:31