Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Touch events on UITableView's backgroundView

I've got a UITableViewController, and the tableView has a view as backgroundView (even more, it's a MKMapView), set with

self.tableView.backgroundView = _mapView;

Currently the background view is showing on the screen (I also have a transparent table header to achieve this). I'm not able to make the mapView (tableView's backgroundView) respond to user interaction. My table header, which is above the map view, is a subclass of UIView with the following override:

- (UIView *)hitTest:(CGPoint)point withEvent:(UIEvent *)event
{
    LogDebug(@"Clic en transparent view");
    if (_transparent)
    {
        return nil;
    }
    return [super hitTest:point withEvent:event];
}

so it's supposed to pass through the events; I don't know what's wrong, and didn't find any solution in other similar questions.

I must keep the mapView as the tableView's background property, so please take this into account.

Any idea?

like image 238
sonxurxo Avatar asked Jul 06 '26 20:07

sonxurxo


1 Answers

it's relatively late to answer the question. But I ran into this problem on my own project, and after some research I got the answer. First off, I didn't assign the view in the back to tableView.backgroundView. Instead, I made the tableView.backgroundColor transparent then put another view under the table view.

EDIT: by "under the table" I mean the tableView and the anotherView are children to the same parent view and tableView.zPosition is greater than anotherView.zPosition.

Then all I did is override the tableView:

(BOOL)pointInside:(CGPoint)point withEvent:(UIEvent *)event

and let the tableView avoid holding the events that are not in its cells.

Here's the code:

-(BOOL)pointInside:(CGPoint)point withEvent:(UIEvent *)event {

    /// the height of the table itself.
    float height = self.bounds.size.height + self.contentOffset.y;
    /// the bounds of the table. 
    /// it's strange that the origin of the table view is actually the top-left of the table.
    CGRect tableBounds = CGRectMake(0, 0, self.bounds.size.width, height);
    return CGRectContainsPoint(tableBounds, point);

}

it works like a charm for me. hope it'll help any other people who run into this problem.

like image 200
alvisjiang Avatar answered Jul 09 '26 20:07

alvisjiang



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!