Reputation: 3582
I just got this bug report for me app...the activity view controller is suddenly this weird narrow shape whether I'm on an actual phone or the view controller.
This is happening with some plain vanilla code that hasn't been touched in months:
UIActivityViewController *activityViewController = [[UIActivityViewController alloc] initWithActivityItems:@[message] applicationActivities:nil];
[self presentViewController:activityViewController animated:YES completion:nil];
What could be going wrong? I can't even think of where to start troubleshooting this one.
Upvotes: 9
Views: 1983
Reputation: 85
We would need to set the sourceView and sourceRect (optional but may needed to be set for iPad).
We may try below snippet
activityViewController.popoverPresentationController?.sourceView = sender.self
activityViewController.popoverPresentationController?.sourceRect = sender.frame
Upvotes: 0
Reputation: 4917
In some cases it may happen.
NSArray *Items = [NSArray arrayWithObjects:
@"Checking Test App", nil];
UIActivityViewController *activity=[[UIActivityViewController alloc]initWithActivityItems:Items applicationActivities:nil];
[self presentViewController:activity animated:YES completion:nil];
or
NSString *string = NSLocalizedString(@"shareString", nil);
UIActivityViewController *activityViewController = [[UIActivityViewController alloc]
initWithActivityItems:@[string] applicationActivities:nil];
[activityViewController setCompletionWithItemsHandler:
^(NSString *activityType, BOOL completed, NSArray *returnedItems, NSError *error)
{
if ( completed ) {
NSLog(@"sharing complete");
} else
{
NSLog(@"cancelled");
}
}];
[self presentViewController:activityViewController animated:YES completion:^{
}];
Upvotes: 2
Reputation: 41
In iPadOS you have to set the PopoverPresentationController's Source View and Source Rect properties. The following code will make it so the ActivityViewController comes from the center of the screen with the default size
For Swift 5
activityViewController.popoverPresentationController?.sourceView = self.view
activityViewController.popoverPresentationController?.sourceRect = CGRect(x: self.view.bounds.size.width / 2.0, y: self.view.bounds.size.height-50, width: 1.0, height: 1.0)
Upvotes: 1
Reputation: 683
I can reproduce this on iOS 12 when:
The solution in my case is to switch self back to UIModalPresentationFullScreen.
Upvotes: 0
Reputation: 2462
If you are on iPad, try setting the popoverPresentationController
property of sourceRect
NSString *string = NSLocalizedString(@"shareString", nil);
UIActivityViewController *activityVC = [[UIActivityViewController alloc] initWithActivityItems:@[string] applicationActivities:nil];
activityVC.popoverPresentationController.sourceView = self.view;
activityVC.popoverPresentationController.sourceRect = CGRectMake(self.view.bounds.size.width / 2.0, self.view.bounds.size.height-50, 1.0, 1.0);
[self presentViewController:activityVC animated:YES completion:nil];
Upvotes: 2
Reputation: 1895
Have you tried reproducing this bug??? If we don't have the exact scenario of how this bug is getting created then we can't make a solution for it!!! Try updating the items in the array and check if the bug still exist... This type of things happens sometimes but until and unless we don't reproduce this scenario it can't be termed as bug.
Any way if you are working in universal app add below line of code before presenting the ActivityView
ActivityViewController.popoverPresentationController.sourceView = self.view;
[self presentViewController:activityViewController animated:YES completion:nil];
I will suggest, instead of looking for a solution dig out the problem first. I have never seen such weird behaviour of ActivityView before this and if we know why this happened then it will be helpful to every iOS developer.
Upvotes: 2
Reputation: 4179
These kinds of bugs do occur and they really test your patience and give you a challenge. Although I cannot give a definite answer, I can give you some tips on how to troubleshoot this!
What I suggest is that you first try to re-create this on your development machine. Then try to play around and see what's causing the issue. Here are some things to try out.
Run this on several devices with different OS's so that you might be able to determine a pattern.
Try and change the "initWithActivityItems
" value(s) and see
whether the problem occurs.
See whether the issue exists if you try to create the ActivityViewController from a different view controller too.
Go through your code and see if there are any warnings that you have simply ignored. Specially if you're using Storyboards for creating the view.
I know this is not an answer, but I cannot post such a long response as a comment.
Hope this helps!
Upvotes: 1
Reputation: 24248
Maybe self.view.frame.size.width
is the problem. You can find your the frame with NSLog
self.view
. Simple workaround could be:
[self.view.window.rootViewController presentViewController:activityViewController animated:YES completion:nil];
Upvotes: 1
Reputation: 791
You should check frames for your self's view. Probably its width less than the width of the activityViewController and you get this bug.
Upvotes: 1