iPhone/iPad context menu

Im talking about the menu that shows up when you select a block of text it gives you the option to cut/paste/copy. I figured out how to add one more option to the menu, but if I add two or more options it will say “more” first. clicking it will show all the options I added. But is there a way to show all the options I added upfront? without the “more” menu item?

  • How to disable iPhone 6 native resolution?
  • UITableView Detecting Last Cell
  • Reload UICollectionViewCell which has image
  • convert NSData Length from bytes to megs
  • How do I use UISearchController in iOS 8 where the UISearchBar is in my navigation bar and has scope buttons?
  • How to scroll to a particluar index in collection view in swift
  • 2 Solutions Collect From Internet About “iPhone/iPad context menu”

    You need to use a UIMenuController. If you don’t want Copy/Paste/Cut, you’ll include something like this in your canPerformAction: method:

    - (BOOL)canPerformAction:(SEL)action withSender:(id)sender {
         if(action == @selector(someSelector:))
             return YES;
         else 
             return NO;
    }
    

    Creating a new menu item looks like this:

    UIMenuItem *someAction = [[UIMenuItem alloc]initWithTitle:@"Something" action:@selector(doSomething:)];
    
    UIMenuController *menu = [UIMenuController sharedMenuController];
    menu.menuItems = [NSArray arrayWithObject:someAction];
    [menu update];
    

    I assume your talking about UIMenuController. If you don’t want to see Copy/Paste/Cut/Delete/Select/SelectAll you will need to add the following code to your UITextField‘s or UITextView‘s delegate:

    - (BOOL)canPerformAction: (SEL)action withSender: (id)sender {
        BOOL answer = NO;
        if (action == @selector(item1)) {
            answer = YES;
        }
        if (action == @selector(item2)) {
            answer = YES;
        }
        return answer;
    }
    

    Where item1 and item2 are the names of the objects in UIMenuController.menuItems.

    In my experience if you are using a UITextView the Copy, Paste, Cut and Select All menu items will remain, in this case add the following code to a subclass of UITextView.

    - (BOOL) canPerformAction:(SEL)action withSender:(id)sender {
        if (action == @selector(cut:) || action == @selector(copy:) || action == @selector(paste:) || action == @selector(selectAll:)) {
                return YES;
        }
    }