How can I click a button behind a transparent UIView? How can I click a button behind a transparent UIView? ios ios

How can I click a button behind a transparent UIView?


Create a custom view for your container and override the pointInside: message to return false when the point isn't within an eligible child view, like this:

Swift:

class PassThroughView: UIView {    override func point(inside point: CGPoint, with event: UIEvent?) -> Bool {        for subview in subviews {            if !subview.isHidden && subview.isUserInteractionEnabled && subview.point(inside: convert(point, to: subview), with: event) {                return true            }        }        return false    }}

Objective C:

@interface PassthroughView : UIView@end@implementation PassthroughView-(BOOL)pointInside:(CGPoint)point withEvent:(UIEvent *)event {    for (UIView *view in self.subviews) {        if (!view.hidden && view.userInteractionEnabled && [view pointInside:[self convertPoint:point toView:view] withEvent:event])            return YES;    }    return NO;}@end

Using this view as a container will allow any of its children to receive touches but the view itself will be transparent to events.


I also use

myView.userInteractionEnabled = NO;

No need to subclass. Works fine.


From Apple:

Event forwarding is a technique used by some applications. You forward touch events by invoking the event-handling methods of another responder object. Although this can be an effective technique, you should use it with caution. The classes of the UIKit framework are not designed to receive touches that are not bound to them .... If you want to conditionally forward touches to other responders in your application, all of these responders should be instances of your own subclasses of UIView.

Apples Best Practise:

Do not explicitly send events up the responder chain (via nextResponder); instead, invoke the superclass implementation and let the UIKit handle responder-chain traversal.

instead you can override:

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

in your UIView subclass and return NO if you want that touch to be sent up the responder chain (I.E. to views behind your view with nothing in it).