从UIMenuController删除复制,查找和共享 [英] Removing Copy, Look Up, and Share from UIMenuController
问题描述
我试图覆盖默认的UIMenuController
,以便当用户在其文本视图中选择文本时,仅出现我的自定义项目"Define ...".到目前为止,我在网上发现的方法还不太运气.
I am trying to override the default UIMenuController
so that only my custom item "Define..." appears when the user selects text in its text view. I haven't had much luck with the approaches I've found online thus far.
更具体地说,我将UIViewController
子类化,并使用canPerformAction()
排除了我的define方法之外的所有动作.
To be more specific, I have subclassed a UIViewController
and used canPerformAction()
to exclude all actions except my define method.
override func becomeFirstResponder() -> Bool {
return true
}
override func canPerformAction(_ action: Selector, withSender sender: Any?) -> Bool {
let canPerform: Bool
if action == #selector(defineWord){
canPerform = true
}
else {
canPerform = false
}
print("action = \(action), canPerform = \(canPerform)")
return canPerform
}
在视图控制器的viewDidLoad()
中,我包括了以下内容:
In the view controller's viewDidLoad()
, I've included the following:
let shared = UIMenuController.shared
let menuItemDefine = UIMenuItem(title: "Define...", action: #selector(self.defineWord))
shared.menuItems = [menuItemDefine]
每当我在视图中选择文本时,控制台都会遍历UIMenuController
中可能出现的每种可能的操作,并说除了我的自定义操作之外,它们无法执行:
Whenever I select text in the view, the console goes through each possible action that might appear in the UIMenuController
and says they can't be performed, with the exception of my custom action:
action = cut:, canPerform = false
action = select:, canPerform = false
(and so on, until...)
action = defineWord, canPerform = true
但是出现的编辑菜单包含复制",查找",共享"和定义...".这些没有出现在控制台中,这使我认为需要使用其他方法.
But the resulting edit menu contains "Copy", "Look Up", "Share", and "Define...". These don't appear in the console, which makes me think that a different approach is called for.
请注意,我还尝试了对UITextView
进行子类化,并适当地使用上述代码,但是结果是相同的.
Note that I've also tried subclassing UITextView
and using the above code as appropriate, but the result is the same.
有什么想法我要去哪里吗?
Any ideas where I'm going wrong?
推荐答案
这可能会帮助所有提出以下问题的人:如何删除复制",全选"等.标准菜单项或UIResponderStandardEditActions
当您已经在canPerformAction:
中返回false
时,仍会看到.
This might help everyone who is asking this question that how to remove "Copy", "Select All" etc.. standard menu items or UIResponderStandardEditActions
that are still visible when you have already returned false
in canPerformAction:
.
它与响应者链有关.由于每个响应者都调用canPerformAction:
,因此对于某些响应者,它可能在canPerformAction:
中作为默认值返回true.
It is related to responder chain. As canPerformAction:
is called for every responder, for some of those it may be returning true in canPerformAction:
as a default value.
因此要检查它在哪里失败,我对控制器中使用的每个元素都覆盖了此canPerformAction:
,从而找到了它
Thus to check where it is failing I found it by overriding this canPerformAction:
for every element I used in my controller
例如,在我的视图控制器中,我有一个webview
,而我所做的错误是我在委托方法中覆盖了canPerformAction:
,即我正在执行以下操作
For example in my view controller I had a webview
and the mistake I was doing was that I was overriding the canPerformAction:
in the delegate methods i.e I was doing something like below
extension viewcontroller: UIWebViewDelegate{
open override func canPerformAction(_ action: Selector, withSender sender: Any?) -> Bool {
return false
}
}
但是要点是,您必须为元素而不是作为委托方法.
extension UIView {
func dropRoundCorners() {
self.layer.cornerRadius = 10.0;
self.clipsToBounds = true;
}
open override func canPerformAction(_ action: Selector, withSender sender: Any?) -> Bool {
return false
}
}
extension UIImageView{
open override func canPerformAction(_ action: Selector, withSender sender: Any?) -> Bool {
return false
}
}
extension UIScrollView{
open override func canPerformAction(_ action: Selector, withSender
sender: Any?) -> Bool {
return false
}
}
extension UISlider{
open override func canPerformAction(_ action: Selector, withSender
sender: Any?) -> Bool {
return false
}
}
extension UIWebView{
open override func canPerformAction(_ action: Selector, withSender
sender: Any?) -> Bool {
return false
}
}
我希望这对那些坚持此问题的人有用.
I hope this is useful to anyone whose is stuck with this issue.
以下是可能有助于您了解详细信息的链接:
Following are links that might help you with details:
这篇关于从UIMenuController删除复制,查找和共享的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!