UIButton does not detect touch events when clicked

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;

        break;
    default:
        break;
}    
}

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

Answers


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.


Need Your Help

max value if no values are none - python

python python-2.7 list-comprehension

I would like to return a item in a list if certain conditions are met. One condition is that my the Project is "Project A", but the other is that t[1] is not None for all sublists that contain "Pro...

Save $_SESSION from a list of checkbox data?

php

Im trying to save $_SESSION data to keep checked values on page refresh, I have some code written up but I am missing a piece.

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.