我看过很多回复,但仍然无法弄清楚。我知道第一行有点奇怪,但它是因为它继承自将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];
答案 0 :(得分:8)
这是你的答案: searchBar.userInteractionEnabled = NO; 如果要将子视图添加到禁用了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);
按钮完全不可见,标签相同,对象放置在界限之外,这就是你无法触摸它们的原因。
答案 1 :(得分:7)
userInteractionEnabled设置为NO将向下级联到所有子视图。因此,您的UIButton实例将显示无响应,因为searchBar已禁用。