Disable touches on UIView background so that buttons on lower views are clickable
You're close. Don't override -hitTest:withEvent:
. By the time that is called, the event dispatcher has already decided that your subtree of the hierarchy owns the event and won't look elsewhere. Instead, override -pointInside:withEvent:
, which is called earlier in the event processing pipeline. It's how the system asks "hey view, does ANYONE in your hierarchy respond to an event at this point?". If you say NO, event processing continues below you in the visible stack.
Per the documentation, the default implementation just checks whether the point is in the bounds of the view at all.
Your strategy is to say "yes" when any of your subviews is at that coordinate, but say "no" when the touch would be hitting the background.
- (BOOL)pointInside:(CGPoint)point withEvent:(UIEvent *)event
{
for (UIView * view in [self subviews]) {
if (view.userInteractionEnabled && [view pointInside:[self convertPoint:point toView:view] withEvent:event]) {
return YES;
}
}
return NO;
}
Thanks to @Ben Zutto, Swift 3 solution:
override func point(inside point: CGPoint, with event: UIEvent?) -> Bool {
for view in self.subviews {
if view.isUserInteractionEnabled, view.point(inside: self.convert(point, to: view), with: event) {
return true
}
}
return false
}