3

Found several videos how to use popovers and tried to make the same with just storyboard, so I got this it it:

enter image description here

But, with the app running, when I push the button this little orange controller takes all the green screen and not looks like a small popover with an arrow as I expected.

mugx
  • 9,869
  • 3
  • 43
  • 55
wm.p1us
  • 2,019
  • 2
  • 27
  • 38

2 Answers2

6

To be able to show such ViewController (the orange one) in a popover, you have to define the modalPresentationStyle as popover doing so:

class ParentViewController: UIViewController, UIPopoverPresentationControllerDelegate {
    func adaptivePresentationStyle(for controller: UIPresentationController) -> UIModalPresentationStyle {
        return .none
    }

    override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
        if segue.identifier == "PopoverSegue" {
            let popoverVc = segue.destination
            popoverVc.modalPresentationStyle = .popover
            popoverVc.popoverPresentationController?.delegate = self;
            popoverVc.preferredContentSize = CGSize(width: 250, height: 250)
        }
    }
}

remember to set the segue identifier (PopoverSegue or whatever) in the interface builder:

enter image description here

the following freeform size (ignored at runtime), will be important to simulate your popover view inside the interface builder:

enter image description here

final result is:

enter image description here

mugx
  • 9,869
  • 3
  • 43
  • 55
  • It almost what I wanted but it has wrong size (in storyboard I set 250x250 but it is not the same now) and this window is not centered for some reason. – wm.p1us Jan 21 '18 at 08:29
  • @wm.p1us I see, check my edit: `popoverVc.preferredContentSize = CGSize(width: 250, height: 250)` – mugx Jan 21 '18 at 08:36
  • So the size from ib is ignored? – wm.p1us Jan 21 '18 at 08:41
  • Yes. Anyway, since you'll have to setup constraints and so on, such size (freeform) will be important to simulate your view inside the interface builder. – mugx Jan 21 '18 at 08:56
0

Adjust size , arrow and sourceRect as you want

 override func prepare(for segue: UIStoryboardSegue, sender: Any?) {

   var des = segue.destination

    des.modalPresentationStyle = UIModalPresentationStyle.popover

    des.popoverPresentationController?.permittedArrowDirections = .left

    des.popoverPresentationController?.delegate = self

    des.popoverPresentationController?.sourceView = self.view

    des.popoverPresentationController?.sourceRect = CGRect.init(x: 300, y: 0, width: 50, height: 8 )

    des.preferredContentSize = CGSize.init(width: 200, height: 200)

}
Shehata Gamal
  • 98,760
  • 8
  • 65
  • 87