Search code examples
iosobjective-cios8uimenucontrolleruimenuitem

How to disable Copy & Define UIMenuItems of UIMenuController in UITextfield


I am implementing custom UIMenuController and trying to figure out. How can I legally disable "Copy" and "Define" UIMenuItems of UIMenuController in UITextfield? Textfield is not editable. I tried to disable "Copy" using:

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

    return [super canPerformAction:action withSender:sender];
}


- (IBAction)tapTextViewGesture:(id)sender {

  UIMenuItem *myItem1 = [[UIMenuItem alloc] initWithTitle:@"myItem1" action:@selector(myItem1Pressed:)];
  UIMenuItem *myItem2 = [[UIMenuItem alloc] initWithTitle:@"myItem2" action:@selector(myItem2Pressed:)];
  UIMenuItem *myItem3 = [[UIMenuItem alloc] initWithTitle:@"myItem3" action:@selector(myItem3Pressed:)];

    // Access the application's shared menu
    UIMenuController *menu = [UIMenuController sharedMenuController];

    [menu setMenuItems:[NSArray arrayWithObjects:myItem1,myItem2,myItem3, nil]];

    CGRect menuRect = CGRectMake(20, 50, 200, 0);


    // Show the menu from the cursor's position
    [menu setTargetRect:menuRect inView:self.view];


    [menu setMenuVisible:YES animated:YES];
}

But the menu is still showing "Copy" and "Define" UIMenuItems. How can I disable them, leaving only my items?


Solution

  • Finally solved it by subclassing UITextView (created custom class for it) and just added

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

    inside of .m file of my custom TextView subclass.

    After that "Copy" doesn't appear any more, with or without [menu update];