简体   繁体   中英

Increase hit area for UITableView

I'm trying to have a UITableView that is in grouped style that takes up around 70% of the width of the iPad, as 100% just doesn't look good. This means there's around 150px either side.

This is fine, but I want to have the whole width of the screen available to scroll.

I've tried all sorts of mucking around with the content view and insets, etc, such as:

self.theTable.contentInset = UIEdgeInsetsMake(0, 150, 0, 150);
self.theTable.contentSize = CGSizeMake(self.theTable.contentSize.width + 300,  self.theTable.contentSize.height);
self.theTable.contentOffset = CGPointMake(-150, 0);

But I can't make it work like I want to.

I need to be able to tap the background on either side (the 150px or so) and be able to scroll the table view by dragging there.

Essentially, I guess this equates to either increasing the scroll view size by 150px either side, or insetting the tableview 150px either side.

Any ideas?

You can override hitTest:withEvent: method of tableView's superview and return tableView for desired points.

Alternatively you can override hitTest:withEvent: in tableView and check conditions there.

An example of containers superview hitTest method. All events will be forwarded to tableView.

- (UIView *)hitTest:(CGPoint)point withEvent:(UIEvent *)event {
    if (![self pointInside:point withEvent:event]) {
        return nil;
    }
    UITableView *tableView = nil;
    for (UIView *v in self.subviews) {
        if ([v isKindOfClass:[UITableView class]]) {
            tableView = v;
            break;
        }
    }

    if (!tableView) {
        return self;
    }

    if (CGRectContainsPoint(tableView.frame, point)) {
        return [tableView hitTest:[self convertPoint:point toView:tableView] withEvent:event];
    } else {
        return tableView;
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM