Ios 为什么UIButton对触摸没有反应?

Ios 为什么UIButton对触摸没有反应?,ios,uibutton,Ios,Uibutton,我看了很多回复,但仍然无法理解。我知道第一行有点奇怪,但这是因为它继承了一个将headerview设置为搜索栏的超类 searchBar = (UISearchBar *)self.tableView.tableHeaderView; [[searchBar.subviews objectAtIndex:0] removeFromSuperview]; UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(0, -100, sel

我看了很多回复,但仍然无法理解。我知道第一行有点奇怪,但这是因为它继承了一个将headerview设置为搜索栏的超类

searchBar = (UISearchBar *)self.tableView.tableHeaderView;
[[searchBar.subviews objectAtIndex:0] removeFromSuperview];

UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(0, -100, self.view.frame.size.width, 100)];
label.text = @"AAAA";
[searchBar addSubview:label];

UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[button addTarget:self
           action:@selector(aMethod)
 forControlEvents:UIControlEventTouchDown];
[button setTitle:@"Show View" forState:UIControlStateNormal];
button.frame = CGRectMake(80.0, -60, 160.0, 40.0);

searchBar.exclusiveTouch = NO;
searchBar.userInteractionEnabled = NO;
label.userInteractionEnabled = NO;
button.userInteractionEnabled = YES;
[searchBar insertSubview:button atIndex:3];

父视图上设置为“否”的userInteractionEnabled将级联到所有子视图。因此,您的UIButton实例将显示为无响应,因为搜索栏已禁用。

以下是您的答案: searchBar.userInteractionEnabled=否; 如果要将子视图添加到已禁用userInteraction的视图中,则该子视图将不会收到触摸。我没有仔细检查你的代码,看看是否还有其他错误

首先看一下框架:

button.frame=CGRectMake(80.0,-60,160.0,40.0)

UILabel*label=[[UILabel alloc]initWithFrame:CGRectMake(0,-100,self.view.frame.size.width,100);


按钮根本看不见,标签也一样,对象被放置在边界之外,这就是你不能触摸它们的原因。

尝试设置
searchBar.userInteractionEnabled=YES;
instead我已经尝试过了,但它不起作用也尝试过,不起作用即使我删除了searchBar,它仍然不起作用sive touch和searchbar.UserInteractionEnabled是的,它们被放置在边界之外。不能将触摸发送到边界之外的对象吗?它们仍然可见,因为我正在以偏移量显示tableview。编辑:这是可行的。不,触摸不能发送到那里,就像您试图在屏幕右侧移动图像,只看到一个屏幕拐角处,你正在尝试看看外面有什么。我已经向WWDC的一位iOS工程师询问过这个问题。显然,如果你重写一个方法,这是可以做到的。但在这种情况下不值得。谢谢。我认为你指的是hitTest。但是,是的,不值得。