UIView -- "user interaction enabled" false on parent but true on child?
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.
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)
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>
}