iOS——是否可以强制 UILabel 子类对象成为第一响应者?
有什么办法可以做到这一点吗?
我尝试将以下内容放入子类中:
- (BOOL)canBecomeFirstResponder
{
return YES;
}
但是当我向对象发送成为FirstResponder消息时,它仍然没有成为第一响应者。
Is there any way to do this?
I tried putting the following into the subclass:
- (BOOL)canBecomeFirstResponder
{
return YES;
}
But when I sent the object a becomeFirstResponder message, it still didn't become the first responder.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
是的,这是可能的。您应该:
重写
becomeFirstResponder
以返回YES
。将
userInteractionEnabled
设置为YES
。添加
UITapGestureRecognizer
来处理点击。从点击处理程序调用
becomeFirstResponder
。您甚至可以覆盖
inputView
以在屏幕底部获取输入控件。否则就什么都没有了。Yes, it is possible. You should:
Override
becomeFirstResponder
to returnYES
.Set
userInteractionEnabled
toYES
.Add a
UITapGestureRecognizer
to handle taps.Call
becomeFirstResponder
from the tap handler.You can even override
inputView
to get a input control at the bottom of the screen. Otherwise there will be nothing.我没有明确的答案,只有一些(也许是疯狂的)想法:您是否也尝试过重写
becomeFirstResponder
以返回YES
?它的userInteractionEnabled
是否也设置为YES
?否则,将其设为自定义 UIButton 而不是自定义 UILabel...
I have no definite answer, just a few (maybe crazy) ideas: Have you also tried to override
becomeFirstResponder
to returnYES
? Does it haveuserInteractionEnabled
also set toYES
?Otherwise, make it a custom UIButton instead of a custom UILabel...
这对我有用。
子类 UILabel 并重写这些方法:
将手势添加到标签以检测点击。
处理手势事件,呈现一个带有所需框架的 UIMenuController:
This worked for me.
Subclass UILabel and override these methods:
Add Gesture to your label to detect tap.
Handle gesture event, present a UIMenuController with the desired frame: