UIView — “user interaction enabled” false on parent but true on child?

前端 未结 6 562
北海茫月
北海茫月 2020-12-08 13:02

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?

相关标签:
6条回答
  • 2020-12-08 13:18

    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>
    }
    
    0 讨论(0)
  • 2020-12-08 13:26

    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.

    0 讨论(0)
  • 2020-12-08 13:35

    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;
        }
    }
    
    0 讨论(0)
  • 2020-12-08 13:38

    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 Disable touches on UIView background so that buttons on lower views are clickable 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)

    0 讨论(0)
  • 2020-12-08 13:42

    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!

    0 讨论(0)
  • 2020-12-08 13:42

    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

    0 讨论(0)
提交回复
热议问题