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.

Andrea Mugnaini
  • 9,425
  • 3
  • 40
  • 50
wm.p1us
  • 1,971
  • 1
  • 26
  • 35

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

Andrea Mugnaini
  • 9,425
  • 3
  • 40
  • 50
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)

}
Sh_Khan
  • 93,445
  • 7
  • 57
  • 76