Show the UIMenuController and Display Custom Edit Menus for UIViewController, UITableViewController, and UICollectionView on iOS 7

Menu Popup for Copy and Paste using UIMenuController

The UIMenuController is an easy way to give the user copy/paste menu features in an app. You can also use it to expose custom features like saving, viewing, or deleting content. It can be a little tricky to setup and show the menu, so I'll show you how to get started.

We'll need to implement some methods from the UIResponder class in order to enable the popup UIMenuController to appear. Missing any of the lines of code below means that you won't see anything.

UIResponder Class Methods

Managing the Responder Chain

Validating Commands

UIMenuController Video Tutorial

UIMenuController Code Tutorial

I like to use tap gestures because they're quick to setup and are fun to use in iPhone apps. All we need to do is add a gesture for the view and then we can display a menu for the current UIViewController. Add the UITapGestureRecognizer in your viewDidLoad method and then we'll implement all the UIResponder methods and our custom action methods.

- (void)viewDidLoad
{
    [super viewDidLoad];

    UITapGestureRecognizer *tapGesture =
      [[UITapGestureRecognizer alloc] initWithTarget:self
                                              action:@selector(handleTapGesture:)];
    [self.view addGestureRecognizer:tapGesture];
}


- (void)handleTapGesture:(UITapGestureRecognizer *)tapGesture {
    NSLog(@"tapGesture:");
//    CGRect targetRectangle = self.tapView.frame;
    CGRect targetRectangle = CGRectMake(100, 100, 100, 100);
    [[UIMenuController sharedMenuController] setTargetRect:targetRectangle
                                                    inView:self.view];

    UIMenuItem *menuItem = [[UIMenuItem alloc] initWithTitle:@"Custom Action"
                                                  action:@selector(customAction:)];

    [[UIMenuController sharedMenuController]
     setMenuItems:@[menuItem]];
    [[UIMenuController sharedMenuController]
     setMenuVisible:YES animated:YES];
    
}

- (BOOL)canBecomeFirstResponder {
    return YES;
}

- (BOOL)canPerformAction:(SEL)action
              withSender:(id)sender
{
    BOOL result = NO;
    if(@selector(copy:) == action ||
       @selector(customAction:) == action) {
        result = YES;
    }
    return result;
}

// UIMenuController Methods

// Default copy method
- (void)copy:(id)sender {
    NSLog(@"Copy");
}

// Our custom method
- (void)customAction:(id)sender {
    NSLog(@"Custom Action");
}

UITableView and UICollectionView Require More Code

It's important to understand that if you're doing this on a custom UIView or for your UIViewController subclass that it'll behave differently from a UITableView or UICollectionView.  To work with the UITableView or UICollectionView you'll need to use a few extra methods or nothing will show on screen. These methods give you fine grain control on showing context specific menus based on what content is being displayed in the UITableView or UICollectionView.

UITableViewDelegate Methods

Copying and Pasting Row Content

UICollectionViewDelegate Methods

Managing Actions for Cells

UIWindow and the keyWindow

The UIMenuController can only be displayed on the key window. There are times where you might use someone else's library or framework to display a full-screen advertisement or maybe a mobile printing solution like Sincerely Ship. In these cases I have run into issues where the third-party code has changed key windows, by adding their own UIWindow and didn't reset the key window. This will result in UIMenuController not working, so you can fix it by using this line of code when your viewDidAppear: is called.

[self.view.window makeKeyWindow];

Resources

Let me know if this post was helpful. I spent a lot of time trying to research it and figure out solutions to some of the common problems. You can take a look at the resources that I found helpful.

Connect

  1. Join my iPhone mailing list to keep learning about making iPhone apps.
  2. For more structured learning, signup for my online iPhone app course.