Hi
To present a controller as a popOver we use UIPopoverPresentationController. It display the content in a popover. We can easily do it by setting controller presentation Style as popover and then presenting it.
Add UIPopoverPresentationControllerDelegate on your ViewController
class ViewController: UIViewController , UIPopoverPresentationControllerDelegate{
}
Instantiate a controller from Storyboard and set its modalPresentationStyle as popOver , we can also manage its arrow direction by setting permittedArrowDirections.
let popVc = self.storyboard?.instantiateViewController(withIdentifier: "PopOverViewController") as? PopOverViewController
popVc?.modalPresentationStyle = .popover
let popover = popVc?.popoverPresentationController!
popover?.delegate = self
popover?.permittedArrowDirections = .down // for arrow direction
popVc?.preferredContentSize = CGSize(width: 100, height: 100) //set size of popover
popover?.sourceView = popVc?.view
popover?.sourceRect = CGRect.init(x: 150, y: 200, width: 50 , height: 50) // set the point to display popover
present(popVc!, animated: true, completion:nil) // present PopOver
Once done with the above steps, conform UIPopoverPresentationControllerDelegate method to avoid full screen presentation
func adaptivePresentationStyle(for controller: UIPresentationController, traitCollection: UITraitCollection) -> UIModalPresentationStyle {
return .none
}
To dismiss PopOver add :
self.dismiss(animated: true, completion: {
})
Thanks
0 Comment(s)