8

I am trying to add a custom UIActivity of type UIActivityCategoryAction to UIActivityController. But in iOS 8, all of them are appearing with a black background.

Is there a way to change this?

damirstuhec
  • 6,069
  • 1
  • 22
  • 39
CyberInfo
  • 240
  • 2
  • 9

1 Answers1

22

There are 3 things to note here:

  1. image background,
  2. image opaqueness,
  3. image size.

iOS 7

  1. Image background:

Image background should be transparent.

  1. image opaqueness

The "visible part" of the icon should be non transparent aka opaque. Note that any color information won't be preserved:

  1. image size

Because the image won't be scaled by the system if too small/big, you have to provide appropriately sized image. I found image size 120px x 120px to fit perfectly.

Note: this size also takes icon padding into account.


iOS 8+

  1. Image background:

Image background should be white to match the system UIAction icons but you can also use an arbitrary color.

  1. image opaqueness

Same as in iOS 7, "visible" part of the icon should be non transparent aka opaque, however in iOS 8+ color information will be preserved.

  1. image size

I am using image with size 240px x 240px, but you can apply custom sized image because system will automatically scalle-to-fill image if too small/big.


Wrap up

That said, if you want to support both iOS 7 and iOS 8+, you have to have 2 versions of custom UIActivity icon image.

For iOS 7 you should use 120px x 120px sized image with transparent background. Note: find the size that best suits your needs.

For iOS 8+ you should use custom sized square image with white background and "visible" part of an arbitrary colour.

Code example

- (UIImage *)activityImage
{
    if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 8.0) {
        return [UIImage imageNamed:@"activity_icon_ios8"];
    }
    else {
        return [UIImage imageNamed:@"activity_icon"];
    }
}

Hope that helps!

damirstuhec
  • 6,069
  • 1
  • 22
  • 39
  • 1
    Great answer. Is this documented anywhere? – robinkunde Sep 15 '14 at 14:39
  • 3
    Any info on the icon displayed next to the activity on tapping "More"? – Xcoder Oct 07 '14 at 10:01
  • @rkunde Thanks! I haven't been able to find any documentation on this. I believe they will add it in the future. – damirstuhec Oct 07 '14 at 16:29
  • @Xcoder I see what you mean. I think there is no way of customizing/providing a custom activity icon for "Activities" screen of ``UIActivityController``. At least for now. – damirstuhec Oct 07 '14 at 16:58
  • iOS 8 icons do not need to have a white background. I could use any color, such as green. – Pwner Nov 25 '14 at 00:27
  • @Pwner no one said that you can't use an arbitrary color for background. I said that the background should be white if you want to match the system UIAction icons. – damirstuhec Nov 26 '14 at 13:06
  • I tried creating an image in iOS 9 that was 240x240 and it did not auto-scale down. I had to change it to 120x120 – zeroimpl Feb 20 '16 at 02:25
  • @zerotool I just tried and had no problems using 240x240 with iOS 9.2. It scales properly. I was using 240px x 240px PNG as 2x. – damirstuhec Feb 20 '16 at 08:36
  • Looks like it auto scales down for UIActivityCategoryShare but not UIActivityCategoryAction – zeroimpl Feb 20 '16 at 21:31
  • For settings image there is a solution [here](http://stackoverflow.com/a/35680343/1545278) using `activitySettingsImage`, but it's undocumented at this moment – Balazs Nemeth Aug 24 '16 at 12:18