UIButton key not responding

I added a new UIButton

UIButton *newButton = [UIButton buttonWithType:UIButtonTypeCustom]; [newButton setFrame:CGRectMake(x, y, width, height)]; [newButton setTitle:@"" forState:UIControlStateNormal]; [newButton setTag:x+INDEX_OFFSET]; [newButton setBackgroundImage:image forState:UIControlStateNormal]; [newButton addTarget:self action:@selector(buttonPressed) forControlEvents:UIControlEventTouchUpInside]; [self addSubview:newButton]; [self bringSubviewToFront:newButton]; 

and I see it on the screen, but it does not respond to the gesture of pressing. It should call "buttonPressed". Any help would be super!

+4
source share
2 answers

One possible problem is that you add a button at the frame position from your supervisor. Just to verify this, try setting the clipToBounds property to YES in your viewing restrictions, then run the application. If you do not see the button, this means that you are setting the position of the position above your head, so it does not respond to touch.

+9
source

Another possible problem is that you add the UIButton as a subview to the UIImageView (which you don't need, but it works). In this case, you can either add it to the UIView or set the UIImageView userInteractionEnabled property to YES .

+1
source

All Articles