Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

UIActivityViewController set sourceView to UIBarButtonItem

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,

like image 829
Seyong Cho Avatar asked Dec 12 '15 19:12

Seyong Cho


1 Answers

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)
like image 111
o.shnn Avatar answered Dec 02 '22 18:12

o.shnn