how to create Custom UIMenuController with only custom items other than default?

前端 未结 1 1027
别跟我提以往
别跟我提以往 2021-02-09 20:27

I have requirement to show menu items on uiwebview whenever user selects any text.

I have tried

let highlightMenuItem = UIMenuItem(title: \"Highlight\         


        
1条回答
  •  借酒劲吻你
    2021-02-09 21:22

    You can achieve this by subclassing UIWebView and overriding canPerformAction (Swift 3). Then, all you need to do is return false for whichever actions you want disabled.

    Example:

    class EditedUIMenuWebView: UIWebView {
    
      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)
      }
    
    }
    

    If you have any questions please ask!

    Edit If you want to disable all actions but a few it may be easier to just return false in canPerformAction and return true for the ones you want like so:

    override func canPerformAction(_ action: Selector, withSender sender: AnyObject?) -> Bool {
       if action == #selector(copy(_:)) || action == #selector(customMethod(_:)) {
         return true
       }
       ...
       return false
     }
    

    0 讨论(0)
提交回复
热议问题