Take the 2-minute tour ×

I've created a tableview that has custom cells (I've subclassed UITableViewCell).
I'm creating several UIButtons in it. Declaring this in the .h file:

@property (nonatomic, strong) UIButton *myButton;

and this in the .m file:

myButton = [[UIButton alloc] initWithFrame:CGRectMake(-100.0, 285.0, 60.0, 30.0)];
myButton.titleLabel.font = [UIFont boldSystemFontOfSize:16.0];
myButton.backgroundColor = [UIColor clearColor];
[myButton setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
[myButton setTitle:@"text" forState:UIControlStateNormal];
[myButton addTarget:self action:@selector(myButtonClicked:) forControlEvents:UIControlEventTouchUpInside];
[self.contentView addSubview:myButton];

I'm moving the cell to the right using UIPanGestureRecognizer and then the UIButton is revealed (notice that it is in a negative X index).
I can see it but it is not clickable (I guess it is not in the view any more).
Any suggestion about how to make it clickable?

More details:
In the init of the cell, I'm doing:

UIPanGestureRecognizer *panGesture = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(handlePanGesture:)];
panGesture.delegate = self;
[self.contentView addGestureRecognizer:panGesture];

Then I implemented the gesture:

-(void)handlePanGesture:(UIPanGestureRecognizer *)sender {
    CGPoint translate = [sender translationInView:self.contentView];
    CGRect currentFrame = self.contentView.frame;
switch ([sender state]) {        
    case UIGestureRecognizerStateChanged:

        currentFrame.origin.x = translate.x;
        self.contentView.frame = currentFrame;


When the frame moves to the right - the button is revealed. But not clickable...

share|improve this question
can you give more details and paste some more code? I pasted your code into a tableview I have in one project and changed -100 to 100 to see it and I can click on it. –  jcesar Sep 10 '12 at 13:19
did u identify which ui element is picking up the touch gesture first? –  tiguero Sep 10 '12 at 13:19
@jcesar if the button is between X index of 0 to 320 it works. if it is in index < 0 and dragged into the view - it does not work. –  Code Monkey Sep 10 '12 at 13:27
Are you sure that you have myButtonClicked: in target (seems your UITableViewCell subclass) object you specify (i.e. does self has myButtonClicked: method)? –  MANIAK_dobrii Sep 10 '12 at 13:34
As I mentioned, the button works just fine when between index 0 to 320. It stopped working when in negative index. –  Code Monkey Sep 10 '12 at 13:36
show 1 more comment

1 Answer

up vote 3 down vote accepted

If a button's frame is outside that of its parent's frame, it will not register touches. In this case, you mention that your button stops working when its frame's x location is set to -100. This makes sense because it is outside the touch area.

Your pan gesture you implemented moves the cell's view, so while the button may appear on screen, it is still OUTSIDE of the cell's view, hence it cannot receive touches.

One possible suggestion for fixing this would be to make your cells bigger from the start. So for example, if a cell's frame size is currently 0,0,320,44, you could instead make it -100,0,420,44 and place the contents of your cell accordingly. Now, your button's frame would change to 0,0,x,y and it would be inside of the cell's view and able to receive touches.

share|improve this answer
Yep, I just figured it out myself. The thing is that increasing the cell size did not help. What I turned out to do is to increase the entire tableview frame. Something like: theTableView = [[UITableView alloc] initWithFrame:CGRectMake(-100, 0, 420, 370) style:UITableViewStylePlain]; The painful part is that now I have to move all the elements 100 to the right :-( Thanks for the help! –  Code Monkey Sep 10 '12 at 15:56
Yeah no problem. I figured moving everything 100 to the right would be a bit of a hassle based on how that stuff is usually set up :) –  MikeS Sep 10 '12 at 15:58
I had a similar problem. I had a UIButton whose view was larger than the parent UIView and thus the bottom part of the button would not work. After expanding the height of the parent view all worked! Thanks Mike! –  Flea May 6 '13 at 17:51
add comment

Your Answer


