In Swift, I'm trying to show a popover from a bar button item present in the top right position of navigation bar. Below is my code:
func showOptions(sender: UIBarButtonItem) {
let optionsVC = OptionsViewController(nibName: "OptionsViewController", bundle: nil)
optionsVC.delegate = self
optionsVC.modalPresentationStyle = .popover
optionsVC.preferredContentSize = CGSize(width: 200, height: 200)
present(optionsVC, animated: true, completion: nil)
let popController = optionsVC.popoverPresentationController
popController?.permittedArrowDirections = .up
popController?.delegate = self
popController?.barButtonItem = sender
}
func adaptivePresentationStyle(for controller: UIPresentationController, traitCollection: UITraitCollection) -> UIModalPresentationStyle {
return .none
}
Its working good in iPad and not in iPhone. I've gone through the documentation and different web pages. Everything seems to be right. What's missing in my code?
A popover is a transient view that appears above other content onscreen when you tap a control or in an area. Typically, a popover includes an arrow pointing to the location from which it emerged.
In the Bar Button Item section Change the System Item to Action. Next, Drag a new View Controller from the Object Library on to the Storyboard. Drag a Text View from the Object Library at the top of this View Controller. This View Controller will be displayed in the popover.
The only problem here is you are presenting OptionsViewController
before setting its popover delegate. So first set its delegate then call present function.
let popController = optionsVC.popoverPresentationController
popController?.permittedArrowDirections = .up
popController?.delegate = self
popController?.barButtonItem = sender
present(optionsVC, animated: true, completion: nil)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With