Why is UIButton not responding to touches?

ios, uibutton

Solution

Here is your answer : 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. 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);

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.

Problem

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 ``` 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]; ```

Original source