我已经看了很多回复,但仍然无法弄清楚。我知道第一行有点怪异,但这是因为它是从将headerview设置为搜索栏的父类(super class)继承而来的
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];
最佳答案
这是你的答案:
searchBar.userInteractionEnabled =否;
如果要向禁用了userInteraction的 View 中添加 subview ,则该 subview 将不会受到触摸。我还不太擅长查看您的代码,看看是否还有其他错误。
首先看一下框架:
button.frame = CGRectMake(80.0,-60,160.0,40.0);
UILabel * label = [[UILabel alloc] initWithFrame:CGRectMake(0,-100,self.view.frame.size.width,100);
该按钮根本不可见,并且标签相同,对象放置在边界之外的中,这就是为什么您不能触摸它们的原因。