'How to Remove UIMenuController Default Items In Swift

I'm trying to remove the items Look Up & Share... from the UIMenuController. How would I specifically remove the two and keep my custom one. Here is what I've achieved so far:

override func viewDidAppear(_ animated: Bool) {
        super.viewDidAppear(animated)

        // add two custom menu items to the context menu of UIWebView (assuming in contenteditable mode)


        let menuItem1 = UIMenuItem(title: "My Button", action: #selector(myButtonSel))
        UIMenuController.shared.menuItems = [menuItem1]

    }

Here is the canPerformAction I have:

  override func canPerformAction(_ action: Selector, withSender sender: Any?) -> Bool {

        //let shareSelector: Selector = NSSelectorFromString("_share:")

        if webView?.superview != nil {
            if action == #selector(myButtonSel){
                return true
            }
        }

        return super.canPerformAction(action, withSender: sender)
    }

Also for some odd reason, when I try to remove all the default items and keep only my custom, it does not work. Here is the code I attempted for that:

override func canPerformAction(_ action: Selector, withSender sender: Any?) -> Bool {

    //let shareSelector: Selector = NSSelectorFromString("_share:")

    if webView?.superview != nil {
        if action == #selector(myButtonSel){
            return true
        }
        else {

            return false
        }
    }

    return super.canPerformAction(action, withSender: sender)
}

Even when I try to remove all of the other items and keep my custom, I'm not able to do so. All I'm able to do is add my custom item.



Solution 1:[1]

I tried this but it worked for my by subclassing the WebView and overriding canPerformAction method, inside which I manually removed the default options.

override func canPerformAction(_ action: Selector, withSender sender: AnyObject?) -> Bool {
    if action == #selector(cut(_:)) {
      return false
    }
    if action == #selector(paste(_:)) {
      return false
    }
    if action == #selector(select(_:)) {
      return false
    }
    if action == #selector(selectAll(_:)) {
      return false
    }
    ...

    return super.canPerformAction(action, withSender: sender)
  }

I referred to this answer by Ike10 and it had worked for me. Give it a shot.

Solution 2:[2]

Objective C version of removing default items from UIMenuController. The default items are part of UIResponderStandardEditActions! For removing default items make sure to create a subclasss of UITextField or UITextView for the functionality to work otherwise in UiViewController class it will not work.

#import "CustomTextField.h"

@implementation CustomTextField



- (BOOL)canPerformAction:(SEL)action withSender:(id)sender
{
if (action == @selector(captureTextFromCamera:) ||
    action == @selector(delete:) ||
    action == @selector(cut:) ||
    [NSStringFromSelector(action)  isEqualToString:@"_promptForReplace:"] ||
    [NSStringFromSelector(action) isEqualToString:@"_transliterateChinese:"] ||
    [NSStringFromSelector(action) isEqualToString:@"_insertDrawing:"] ||
    [NSStringFromSelector(action) isEqualToString:@"_lookup:"] ||
    [NSStringFromSelector(action) isEqualToString:@"_define:"] ||
    [NSStringFromSelector(action) isEqualToString:@"_translate:"] ||
    [NSStringFromSelector(action) isEqualToString:@"_addShortcut:"] ||
    [NSStringFromSelector(action) isEqualToString:@"_accessibilitySpeak:"] ||
    [NSStringFromSelector(action) isEqualToString:@"_accessibilitySpeakLanguageSelection:"] ||
    [NSStringFromSelector(action) isEqualToString:@"_share:"] )
{
    return  false;
}
NSLog(@"OPtion :- %@",NSStringFromSelector(action));

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


@end

The default available options are below which you want to disable.

cut:
copy:
paste:
delete:
_promptForReplace:
_transliterateChinese:
_insertDrawing:
captureTextFromCamera:
_showTextStyleOptions:
_lookup:
_define:
_translate:
_addShortcut:
_accessibilitySpeak:
_accessibilitySpeakLanguageSelection:
_accessibilityPauseSpeaking:
_share:
makeTextWritingDirectionRightToLeft:
makeTextWritingDirectionLeftToRight:

For swift users please convert it . It is easy to convert.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Umar Farooque
Solution 2 prateek sekhri