当它作为self.view的子视图添加时,我有一个UITextField,用于响应触摸事件。 当我将它作为UIImageView的子视图添加时,它不再响应触摸事件。 我使它成为第一个响应者,并且该字段处于活动状态,并且仍然需要键盘输入。 任何想法有什么不对?
I have a UITextField that responds to touch events when it is added as a subview of self.view. When I add it as a subview of a UIImageView it does not respond to touch events any more. I make it a firstResponder and the field is active and takes keyboard input still. Any ideas what is wrong?
最满意答案
UIImageView状态的文档 :
新的图像视图对象被配置为默认忽略用户事件。 如果要处理UIImageView的自定义子类中的事件,则必须在初始化对象后明确将userInteractionEnabled属性的值更改为YES。
尝试设置imgView.userInteractionEnabled = YES
如果这不起作用,我会尝试在UIView中添加UIImageView和UITextField。 无论如何,这种方法似乎更清洁。
The docs for UIImageView state:
New image view objects are configured to disregard user events by default. If you want to handle events in a custom subclass of UIImageView, you must explicitly change the value of the userInteractionEnabled property to YES after initializing the object.
Try setting imgView.userInteractionEnabled = YES
If this doesn't work, I would try adding the UIImageView and the UITextField in a UIView instead. Anyway, this approach seems cleaner.
更多推荐
发布评论