我们往往喜欢用UIImageView最为我们程序的背景,然后再在其上面添加其他的控件。如果直接拖到控件到UImageView上面,可以发现控件并不是属于UIImageView,而是与其并列属于UIView的。但是用代码加的话发现可以用addSubView:方法给它上面加控件,但是程序运行起来就会发现UIImageView上面的控件(比如UIButton、UITextField)根本无法响应事件。这就要用到它的
userInteractionEnabled 属性。
苹果帮助文档是这样解释的:
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.
userInteractionEnabled
A Boolean value that determines whether user events are ignored and removed from the event queue.
@property(nonatomic, getter=isUserInteractionEnabled) BOOL userInteractionEnabled
Discussion
This property is inherited from the UIView parent class. This class changes the default value of this property to NO.
Availability
- Available in iOS 2.0 and later.
Declared In
UIImageView.h
这就说明UIImageView默认是忽略事件的,不会响应事件的(用代码添加,会发现这个效果)。只有修改它的这个属性为YES它上面的控件才可以响应事件。也可以将其他控件直接加到VIew上面与UIImageView并列。