For an iPhone/iPad app, i have the functionality to share when someone clicks on UIBarButtonItem.
UIActivityViewController * activityVC = [[UIActivityViewController alloc] initWithActivityItems:shareItems applicationActivities:nil];
[self presentViewController:activityVC animated:YES completion:nil];
For iPad however, this code errors because I need to set the sourceView for the activityVC.
So I need to add this code, but set it to the position of UIBarButtonItem.
activityVC.popoverPresentationController.sourceView = SomeUIBarButtonItem;
But this doesn't work since UIBarButtonItem doesn't inherit from UIView(which is really strange for me and I don't get this logic).
Is there some way to set it so that share popover appears pointing the bar button item?
Thanks,
All you need to do is use the popover's barButtonItem property.
So a proper code to support ActivityViewController or UIAlertController on iPads:
popover.barButtonItem = self.navigationItem.rightBarButtonItem;
Another example for a full support for iPads, with UIAlertController (but works just the same with ActivityViewController):
UIPopoverPresentationController *popover = alert.popoverPresentationController;
if (popover)
{
popover.barButtonItem = self.navigationItem.rightBarButtonItem;
popover.permittedArrowDirections = UIPopoverArrowDirectionUp;
}
[self presentViewController:alert animated:YES completion:nil];
Swift 4.2
if let popover = alert.popoverPresentationController {
popover.barButtonItem = self.navigationItem.rightBarButtonItem
popover.permittedArrowDirections = .up
}
self.present(alert, 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