Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

UIButton tap 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 can see it on the screen but it is not responding to a tap gesture. It should call "buttonPressed". Any help would be super!

like image 983
Ernest Avatar asked Dec 21 '22 16:12

Ernest


1 Answers

One of the possible problem is that you are adding the button in a frame position out its superview. Just to check it easily try to set clipToBounds property to YES in its superview bounds, then run the app. If you don't see the button it means that you set the button position out superivew position, that's why it doesn't respond to touches.

like image 88
Andrea Avatar answered Jan 08 '23 12:01

Andrea