I have a UIBarButtonItem
with an image on my navigation controller:
infoItem = UIBarButtonItem(image: infoImage,
style: .plain,
target: self,
action: #selector(infoAction))
navigationItem.rightBarButtonItem = infoItem
When tapping I do:
@objc func infoAction()
{
let popoverContentController = InfoViewController()
popoverContentController.preferredContentSize = CGSize(width: 300, height: 300)
popoverContentController.modalPresentationStyle = .popover
popoverContentController.popoverPresentationController?.delegate = self
self.present(popoverContentController, animated: true, completion: nil)
}
This then calls out to UIPopoverPresentationControllerDelegate
functions:
func prepareForPopoverPresentation(_ popoverPresentationController: UIPopoverPresentationController)
{
popoverPresentationController.permittedArrowDirections = .any
popoverPresentationController.barButtonItem = infoItem <<=====
}
func adaptivePresentationStyle(for controller: UIPresentationController,
traitCollection: UITraitCollection) -> UIModalPresentationStyle
{
return .none
}
There <<=====
I set the barButtonItem
. I now expect the popover arrow to point at the center of this item. But it does not:
Looking at the view hierarchy (below), my image is horizontally centred in the rectangular bar button. It's not at the left side at all, which would explain the popover arrow misalignment.
Any ideas what I'm missing here, or how this can be corrected?