如何删除 UIMenuController 中的 COPY UIMenuItem
我正在开发 iPhone 应用程序并使用 UIWebViews。我计划自定义当我在 UiWebView 中突出显示字符串(复制、全选、剪切等)时弹出的 UIMenuItems。我尝试删除这些菜单项,但无法删除“复制”菜单项。
我需要你们的帮助。
谢谢, 扎尔兹·布兹
I am developing an iPhone application and worked with UIWebViews. I planned to customize the UIMenuItems thats popped up when i highlight strings in UiWebView (Copy, Select All, Cut, etc.). I tried removing those menuItems but i cannot remove the COPY menu item.
I need your help guys.
Thanks,
ZaldzBugz
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
你将不得不进行一些劫持。我什至不知道这是否可能。看起来它可能响应一个名为 UILongPressGestureRecognizer 的手势事件。
您需要找出添加了手势的视图。像这样的东西。
但我不认为手势直接在 UIWebview 上。它位于其子视图之一。
所以它可能在这个子视图上,
如果不在那个子视图上,那么它可能在最上面的视图上,这就是我怀疑的。它是一种未记录的 View 类型,但如果您将其视为我们正在调用的方法的 UIView,那么它可能是安全的。另外,因为您将其视为 UIView,所以据我所知,它并不违反规则。
我没有编写代码片段(未经测试)来尝试从所有视图中删除手势的原因是因为您知道从哪个视图中删除手势非常重要。
删除它后,您需要编写自己的 UILongPressGestureRecognizer,然后将新的 UILongPressGestureRecognizer 添加到相应的视图(它被删除的视图)。
您已经失去了复制选择等所有功能...但现在您可以实现自己的功能。
如果您想要一些旧功能,我想我知道如何恢复它,但是我不能保证这是允许的或安全的。
在删除手势之前,您需要重写一个方法到 UIView.您应该能够在当前的工作类文件(在文件顶部)中执行此操作,
我不能保证任何内容,但这应该记录您视图中调用的每个方法的名称。
运行您的应用程序并执行复制、粘贴、选择、选择所有方法。您的日志应显示按下按钮时调用的方法。当您按下按钮时,它可能会显示多种方法,您可能只需要第一个。
现在,当您创建菜单项时,您知道要为其分配什么选择器。
这一切都只是猜测。希望有帮助。
You're going to have to do some hijacking. I don't even know if it is possible. It looks like it probably responds to a gesture event called UILongPressGestureRecognizer.
You need to find out what view has the gesture added to it. something like this.
but I don't think the gesture is on the UIWebview directly. It is on one of its subviews.
So it might be on this subview
If not on that one then it is probably on the view that is on top the most, which is what I suspect. It is an undocumented View type, but if you treat it as a UIView for the methods we are calling, it is presumably safe. Also, because you are treating it as a UIView, it isn't against the rules as far as I understand.
The reason I didn't write the snippet (which is untested) to try to remove the gesture from all of the views is because it is important you know which view you remove it from.
After you remove it, you need to write your own UILongPressGestureRecognizer and then add the new one to the respective view (the view it was removed from).
You have lost all your functionality of copy select etc... but now you can implement your own.
If you want some of the old functionality, I think I know how you can get it back, but I can't promise this is allowed or safe.
Before you remove the gesture you need to override a method to UIView. You should be able to do it in your current working class file (at the top of the file)
I can't promise anything, but this should log the name of EVERY method that is called in your view.
Run your app and execute the copy,paste,select,select all methods. Your log should show what methods get called when you press the buttons. It may show several methods when you push a button, you probably just want the first.
Now when you create a menuItem you know what selector to assign to it.
This is all just a guess. Hope it helps.
我在这个答案中使用了类似问题的技术,它可以消除复制菜单项。也就是说,在示例中重写 UIWebView 而不是 UITextView。还在视图控制器中编写了一个 canPerformAction:withSender: ,它为我的自定义选择器返回 YES,为 copy: 选择器返回 NO,然后如果选择器不是这些选择器,则返回超级实现的值。请记住在 xib 中设置自定义 UIWebView 子类名称。
I used the technique in this answer to a similar question and it works for ridding of the Copy menu item. That is, override UIWebView instead of UITextView in their example. Also wrote a canPerformAction:withSender: in the view controller that returns YES for my custom selectors and NO for the copy: selector, then returns the value of the super implementation if the selector is none of those. Remember to set the custom UIWebView subclass name in your xib.