简体   繁体   English

为什么UIButton没有回应接触?

[英]Why is UIButton not responding to touches?

I've look at many responses and still cannot figure it out. 我看了很多回复,仍然无法弄清楚。 I know the first line is a little weird, but its because it is inheriting from a super class that sets the headerview to search bar 我知道第一行有点奇怪,但它是因为它继承自将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 set to NO on a parent view will cascade down to all subviews. 在父视图上将userInteractionEnabled设置为NO将向下级联到所有子视图。 Thus, your instance of UIButton will appear unresponsive because the searchBar is disabled. 因此,您的UIButton实例将显示无响应,因为searchBar已禁用。

Here is your answer : searchBar.userInteractionEnabled = NO; 这是你的答案:searchBar.userInteractionEnabled = NO; If you are going to add a subview to a view that has userInteraction disabled then that subview won't receive touches. 如果要将子视图添加到禁用了userInteraction的视图,则该子视图将不会接收到触摸。 I haven't looked very good at your code to see if there are other mistakes. 我没看好你的代码,看看是否还有其他错误。

Look at the frames in the first place : 首先看框架:

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

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

The button isn't visible at all , and the label the same , the objects are placed outside of bounds and that's why you cannot touch them. 按钮根本看不到,标签相同,对象放在边界之外,这就是你无法触摸它们的原因。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM