'UIActivityViewController has a weird size and shape

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.

Narrow 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.



Solution 1:[1]

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.

Solution 2:[2]

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];

Solution 3:[3]

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:^{
}];

Solution 4:[4]

You should check frames for your self's view. Probably its width less than the width of the activityViewController and you get this bug.

Solution 5:[5]

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];

Solution 6:[6]

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!

Solution 7:[7]

I can reproduce this on iOS 12 when:

  1. The controller that is presenting the activityViewController (self in this case) is itself presented with UIModalPresentationOverFullScreen
  2. Somewhere in the presentation chain beneath self, there is another UIActivityAlertViewController.

The solution in my case is to switch self back to UIModalPresentationFullScreen.

Solution 8:[8]

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)

Solution 9:[9]

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

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1
Solution 2 Gianni Carlo
Solution 3 Nimantha
Solution 4
Solution 5 Warif Akhand Rishi
Solution 6 Ruchira Randana
Solution 7
Solution 8 Lucas Perretta
Solution 9 Maverick