iOS 9 introduced a new way of creating Popovers on iPhone. Instead of using the UIPopoverController class we can now use the UIPopoverPresentationController.
Presenting a Popover from a Bar Button Item
We need a content View controller that's displayed inside the Popover and also need to set properties for contentSize
, modalPresentationStyle
and sourceView
.
Action for right BarButtonItem-
@IBAction func addEntity(_ sender: UIBarButtonItem) {
let vc: TableViewController = self.storyboard?.instantiateViewController(withIdentifier: "TableViewController") as! TableViewController
// Preferred Size
vc.preferredContentSize = CGSize(width: 200, height: 200)
vc.modalPresentationStyle = .popover
let popover: UIPopoverPresentationController = vc.popoverPresentationController!
popover.delegate = self
popover.sourceView = self.view
// RightBarItem
popover.barButtonItem = addBarButton
present(vc, animated: true, completion:nil)
}
delegate
Method of UIPopoverPresentationController
-
extension ViewController: UIPopoverPresentationControllerDelegate {
func adaptivePresentationStyle(for controller: UIPresentationController) -> UIModalPresentationStyle {
return .none
}
}
Preview