Search code examples
iphoneiosuibarbuttonitemuitoolbaruiactivityviewcontroller

UIBarButtonItems of a UIToolbar hiding when presenting UIActivityViewController


hiding buttonsThis looks like a bug to me, but maybe someone can think of a workaround?

Basically if you have a custom UIToolbar, its button items will automatically hide when you present a UIActivityViewController, and reappear when you dismiss it. This is only the case on the iPhone. Being that UIActivityViewController doesn't hide the entire screen it just looks weird that buttons disappear behind the dimmed screen.

To replicate, simply start a single view project and use the following code on the view controller:

- (void)viewDidLoad {
    UIToolbar *toolbar = [[UIToolbar alloc] initWithFrame:CGRectMake(0, 0, self.view.bounds.size.width, 40)];
    UIBarButtonItem *button = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAction target:self action:@selector(didTapAction)];
    toolbar.items = [NSArray arrayWithObject:button];
    [self.view addSubview:toolbar];
}

- (void)didTapAction {
    NSArray *items = [NSArray arrayWithObjects:@"Text", nil];
    UIActivityViewController *sharing = [[UIActivityViewController alloc] initWithActivityItems:items applicationActivities:nil];
    [self presentViewController:sharing animated:YES completion:nil];
}

Solution

  • Found a workaround. Simply get rid of all the items before presenting, and add them back right after.

    - (void)didTapAction {
        NSArray *items = [NSArray arrayWithObjects:@"Text", nil];
        UIActivityViewController *sharing = [[UIActivityViewController alloc] initWithActivityItems:items applicationActivities:nil];
        NSArray *barItems = toolbar.items;
        toolbar.items = nil;
        [self.navigationController presentViewController:sharing animated:YES completion:nil];
        toolbar.items = barItems;
    }