代码之家  ›  专栏  ›  技术社区  ›  MGames

如何在Swift中删除UIMenuController默认项

  •  0
  • MGames  · 技术社区  · 7 年前

    我正在尝试删除这些项目 查阅 共有

    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]
    
        }
    

    以下是我的简历:

      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)
        }
    

    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)
    }
    

    即使当我试图删除所有其他项目,并保持我的习惯,我不能这样做。我所能做的就是添加我的自定义项。

    2 回复  |  直到 7 年前
        1
  •  2
  •   Umar Farooque    7 年前

    我尝试了这个方法,但它通过将WebView子类化并重写 canPerformAction

    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)
      }
    

    我提到 this 答复人 这对我很有效。试试看。

        2
  •  0
  •   prateek sekhri    2 年前

    UIResponderStandardEditActions ! 要删除默认项,请确保为功能创建UITextField或UITextView的子类,否则在UiViewController类中它将不起作用。

    #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
    

    默认可用选项位于要禁用的选项下方。

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