在UI中,基本上所有的控件的用户交互都是打开的,但是也有一些特例,例如label、imageView。
userInteractionEnabled这个属性影响了响应者链的检测过程,所以如果将一个button放在一个label或者imageView上,都需要将label或者imageView的用户交互打开才可以,否则在检测的过程中是检测不到label或者imageView上的子视图的。
#import "RootViewController.h"
@interface RootViewController ()
@end
@implementation RootViewController
- (void)viewDidLoad {
[super viewDidLoad];
UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(100, 100, 200, 200)];
// 打开label的用户交互
label.userInteractionEnabled = YES;
[self.view addSubview:label];
UIButton *button = [UIButton buttonWithType:(UIButtonTypeSystem)];
button.frame = CGRectMake(10, 10, 80, 30);
[button setTitle:@"誓言の真嗳" forState:(UIControlStateNormal)];
[button addTarget:self action:@selector(clickButton:) forControlEvents:(UIControlEventTouchUpInside)];
[label addSubview:button];
}
- (void)clickButton:(UIButton *)button
{
NSLog(@"誓言的真嗳");
}
如果label的用户交互不打开,在视图上可以看到button,但是点击之后没有响应;所以要打开用户交互。
同样的还有imageView,默认交互也是NO,需要时记得打开。。。