Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why is UIButton not responding to touches?

Tags:

ios

uibutton

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];
like image 451
mergesort Avatar asked Jul 02 '13 14:07

mergesort


People also ask

How can I check if UIButton is pressed?

If you want to check If UIButton is Pressed or not, You should handle TouchDown Handler and change button's state to pressed in touchDown hadnler. You can track ToucUpInside method to Change state of button to Not-pressed again.

What is a UIButton?

A control that executes your custom code in response to user interactions.


1 Answers

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.

like image 117
soryngod Avatar answered Oct 11 '22 01:10

soryngod