如何控制 iPhone SDK 中 UITextView 中粘贴菜单的位置
我有一个 UITextView 放置在包含广告的视图下方,当用户尝试粘贴文本时,粘贴菜单会覆盖插入点上方的广告。 我不想鼓励意外点击,因此我正在寻找一种方法使菜单显示在插入点下方而不是上方。 或者也许有一种方法可以欺骗菜单控制器,使其认为文本视图已经位于屏幕顶部。
我尝试在sharedMenuController 上使用setTargetRect:inView: 方法,但这没有效果。 显然文本视图不使用共享菜单控制器。
我尝试将以下行的变体添加到视图控制器的 textViewDidBeginEditing:textView 和 viewDidLoad 方法中,但没有成功。
[[UIMenuController 共享菜单控制器] setTargetRect: CGRectMake(140, 120, 5, 5) inView:self.view];
I have a UITextView placed just below a view containing an ad and when the user tries to paste in text the paste menu overlays the ad which is just above the insertion point. I don't want to encourage accidental clicks so I'm looking for a way to make the menu display below the insertion point instead of above it. Or maybe there's a way to trick the menu controller into thinking that text view is already at the top of the screen.
I've tried using the setTargetRect:inView: method on the sharedMenuController but that has no effect. Apparently the text view doesn't use the shared menu controller.
I've tried adding variations of the following line to my view controller's textViewDidBeginEditing:textView and viewDidLoad methods with no luck.
[[UIMenuController sharedMenuController] setTargetRect: CGRectMake(140, 120, 5, 5) inView:self.view];
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
简短的回答是,你不能这样做。 有时可以,但不是很可靠。 问题是“UITextView 是一个非常特殊的情况,在 iPhone OS 3.0 中为了处理触摸而对其进行子类化实际上是不可能的。”*
解决这个问题的方法是子类化 UIWindow 并重写其 sendEvent 方法,以便它设置您的菜单的目标矩形,然后显示菜单。
然而,有 4 种方法可以调出编辑菜单,并且这只适用于菜单由触摸事件直接触发的情况。 在其他情况下,菜单是由于触摸另一个 ui 元素(例如,选择或复制菜单项,或触摸蓝色选择点)而间接创建的。
由于我无法可靠地控制菜单的位置,因此我决定监听隐藏和显示通知并切换广告视图的用户交互。 但这也不起作用,因为这些通知仅在执行 setMenuVisible 时显示菜单时才会触发。
如果用户按下 UITextView 并按住,则会弹出一个圆形放大镜。 触摸结束后,会出现一个菜单。 对于这些菜单及其子菜单,据我所知,不可能设置它们的 targetRect 或从它们那里获取通知。 我放弃。
我向 Apple 提交了增强请求。 希望他们会在操作系统的未来版本中改变这一点。
The short answer is, you can't do this. You can sometimes, but not very reliably. The problem is that "UITextView is a pretty special case, and subclassing it for the purposes of handling touches is really not possible in iPhone OS 3.0."*
The way around this would be to subclass UIWindow and override its sendEvent method so it sets your menu's target rect and then displays the menu.
However, there are 4 ways to bring up an edit menu and this only works in the case where the menu is triggered directly by a touch event. In the other cases the menu is created indirectly as the result of touching another ui element (like, select or copy menu items, or touching a blue selection dot).
Since I can't reliably control the location of the menu, I decided to listen for hide and show notifications and toggle user interaction for my ad view. But that doesn't work either since those notifications only trigger if the menu was displayed as a result of you executing setMenuVisible.
If the user presses in the UITextView and holds, This brings up a round magnifying glass. When the touch ends, a menu appears. For these menus and their children, as far as I can tell, its impossible to set their targetRect or to get notifications from them. I give up.
I filed an enhancement request with Apple. Hopefully they'll change this in a future version of the OS.