UIView -- "user interaction enabled" false on parent but true on child?

Cocoa TouchIosUiview

Cocoa Touch Problem Overview


It appears that userInteractionEnabled=NO on a parent view will prevent user interaction on all subviews. Is this correct? Is there any way around this?

Cocoa Touch Solutions


Solution 1 - Cocoa Touch

That's correct, userInteractionEnabled set to NO on a parent view will cascade down to all subviews. If you need some subviews to have interaction enabled, but not others, you can separate your subviews into two parent views: one with userInteractionEnabled = YES and the other NO. Then put those two parent views in the main view.

Solution 2 - Cocoa Touch

You can subclass UIView and override hitTest:withEvent: in a way to pass touch events to a view that you specify (_backView):

- (UIView *)hitTest:(CGPoint)point withEvent:(UIEvent *)event {
    UIView *view = [super hitTest:point withEvent:event];

    if (view == self) {
        view = _backView;
    }

    return view;
}

If the touch event was to be handled by this view it would be passed to "_backView" (that can be an IBOutlet so that it can be set using interface builder) ; and if it was to be handled by any child view just return that child (the result of [super hitTest:point withEvent:event];)

This solution is fine as long as you know what view you need to pass the events to; besides don't know if it has problems since we are returning a view (_backView) that is not a subview of the current UIView !! but it worked fine in my case.

A better solution might be the one mentioned in https://stackoverflow.com/questions/3427619/disable-touches-on-uiview-background-so-that-buttons-on-lower-views-are-clickabl There its mentioned to use -pointInside:withEvent: ; compared to previous solution its better in the way that you don't need to specify a '_backView' to receive the events (the event is simply passed to the next view in chain)! drawback might be that we need to perform -pointInside:withEvent: on all subviews (might be of negligible overhead though)

Solution 3 - Cocoa Touch

I just ran into an odd situation. I have a UIView (call this, V), which has a UIButton as a subview. Call this UIButton, button X. Below is the method I'm using for the target/selector of button X. self below is the view V. The sender parameter is button X.

The situation that's causing me an issue is that if I touch another button on my UI (on the nav bar, call this button Y) and then very quickly touch button X, where the the button Y action disables view V, I still get the touch event sent to button X.

- (void) buttonAction: (UIButton *) sender
{
    NSLog(@"superview: %d", sender.superview.userInteractionEnabled);  
    NSLog(@"button itself: %d", sender.userInteractionEnabled);
     
    // <snip>
}

Here's the output:

> 2014-12-19 16:57:53.826 MyApp[6161:960615] superview: 0
> 2014-12-19 16:57:53.826 MyApp[6161:960615] button itself: 1

That is, the button action occurred and the button's superview had user interaction disabled! And the subview still had user interaction enabled!!

For those of you thinking that this seems artificial, on my app's UI, running on an iPad (running iOS 8.1.2), this came up by accident in my use of the app. It was not something I was originally trying to generate.

Thoughts?

My current workaround is given below, but it seems really odd that it's necessary!

- (void) buttonAction: (id) sender
{
    NSLog(@"superview: %d", sender.superview.userInteractionEnabled);  
    NSLog(@"button itself: %d", sender.userInteractionEnabled);
    if (! self.userInteractionEnabled) return;

    // <snip>
}

Solution 4 - Cocoa Touch

I made up a weird solution for this, I had a child view in a tableView cell that I wanted to be touchable but the parent shouldn't have...

Neither of above solutions worked for me, but I found another solution. Go to storyboard and add a tapGestureRecognizer to the parent view to absorb touches on parent view. Problem solved!

Solution 5 - Cocoa Touch

Here is solution Create class that inherited from "UIView" and write func that override "inside point"

class TTSView: UIView {


    
    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
     }

}

then in storyboard or xib, the superview have to use this class

Solution 6 - Cocoa Touch

I´m doing this in a xib which is my "custom alert controller"-view.

for (UIView *subview in self.superview.subviews) {
    
    if (![subview isEqual:self]) {
        subview.userInteractionEnabled = NO;
    }
}

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionmorgancodesView Question on Stackoverflow
Solution 1 - Cocoa TouchlazycsView Answer on Stackoverflow
Solution 2 - Cocoa TouchAliView Answer on Stackoverflow
Solution 3 - Cocoa TouchChris PrinceView Answer on Stackoverflow
Solution 4 - Cocoa TouchAhmadrezaView Answer on Stackoverflow
Solution 5 - Cocoa TouchUcdemirView Answer on Stackoverflow
Solution 6 - Cocoa TouchNeoGER89View Answer on Stackoverflow