如何创建仅默认项以外的自定义项的Custom UIMenuController? [英] how to create Custom UIMenuController with only custom items other than default?
问题描述
每当用户选择任何文本时,我都要求在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屋!