如何创建仅默认项以外的自定义项的Custom UIMenuController? [英] how to create Custom UIMenuController with only custom items other than default?

查看:69
本文介绍了如何创建仅默认项以外的自定义项的Custom UIMenuController?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

每当用户选择任何文本时,我都要求在uiwebview上显示菜单项.

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

我尝试过

let highlightMenuItem = UIMenuItem(title: "Highlight", action: #selector(ViewController.hightlight))

UIMenuController.sharedMenuController().menuItems = [highlightMenuItem]

但是这只会附加更多菜单项和默认的现有菜单项.这样

but this only appends more menu item with default existing one. as this

仅通过菜单项复制",突出显示"和注释",有没有办法实现这一目标?

Is there any way out to achieve this with only menu items Copy, Highlight and Note?

推荐答案

您可以通过将UIWebView子类化并覆盖canPerformAction(Swift 3)来实现.然后,您需要为要禁用的任何操作返回false.

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.

示例:

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!

编辑如果要禁用所有操作,但要禁用一些操作,只需在canPerformAction中返回false并为所需的返回true可能会更容易:

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
 }

这篇关于如何创建仅默认项以外的自定义项的Custom UIMenuController?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆