UIImageView内的UIButton不响应水龙头

我有一个滚动视图,其中有一个图像作为子视图的图像视图,图像视图有一个UIButton作为其子视图之一。 问题是,我无法点击button。 我可以看到button,但我不能点击它。

有人能告诉我什么是我搞砸了吗? 任何帮助是极大的赞赏! 谢谢!

以下是代码:

 scrollView = [[UIScrollView alloc] initWithFrame:[[UIScreen mainScreen] applicationFrame]]; imageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"img.jpg"]]; scrollView.delegate = self; self.view = scrollView; // add invisible buttons [self addInvisibleButtons]; [scrollView addSubview:imageView]; 

addInvisibleButtons具有以下代码:

 UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect]; [button addTarget:self action:@selector(buttonHandler) forControlEvents:UIControlEventAllEvents]; [button setTitle:@"point" forState:UIControlStateNormal]; button.frame = CGRectMake(0.0, 0.0, 40.0, 40.0); [self.imageView addSubview:button]; 

UIImageView默认情况下将userInteractionEnabled设置为NO / false

您将该button添加为图像视图的子视图。 您应该将其设置为YES / true

我可以问你为什么添加无形的UIButtonsUIImageView

看起来像一个不好的做法,注意Interface Builder不允许你添加里面的UIButton

如果您想要使用触摸处理的图像,您可以:

 UIButton * button = [UIButton buttonWithType:UIButtonTypeCustom]; [button addTarget:self action:@selector(buttonHandler) forControlEvents:UIControlEventAllEvents]; [button setTitle:@"point" forState:UIControlStateNormal]; [button setImage:[UIImage imageNamed:@"img.jpg"] forState:UIControlStateNormal]; [button setFrame:CGRectMake(0.0, 0.0, 40.0, 40.0)]; [scrollView addSubview:button]; 

你可以使用addInvisibleButtons实现

 UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom]; [button setBackgroundColor:[UIColor clearColor]]; [button addTarget:self action:@selector(buttonHandler) forControlEvents:UIControlEventAllEvents]; [button setTitle:@"point" forState:UIControlStateNormal]; button.frame = CGRectMake(0.0, 0.0, 40.0, 40.0); self.imageView.userInteractionEnabled = YES; [self.imageView addSubview:button]; 

如果你想让UIButton完全不可见,那么你需要删除一行[button setTitle:@"point" forState:UIControlStateNormal]; 因为它用来显示UIButton上的文本,使其可见。

这可能会解决您的问题。