blocking user interaction with underlying views

2020-07-09 08:39发布

问题:

In my app, i have an overlay view, which is displayed when some processing and networking is happening. It is just a semitransparent view (subclass of UIView) with a loading indicator on it, filling the whole screen.

I want to prevent any of the underlying views to recieve user-interaction (e.g. a underlying table view should not be scrolled, a button not be pressed).

What is the best way i can do this from within the overlay view?

回答1:

Just set the userInteractionEnabled property for the overlay view to YES. This will cause all touch events to occur on the overlay view and not be passed to the underlying views.



回答2:

Just set property userInteractionEnabled of your semitransparent overlay view to YES.



回答3:

I think the best way is to use these 2 methods:

[[UIApplication sharedApplication] beginIgnoringInteractionEvents];

[[UIApplication sharedApplication] endIgnoringInteractionEvents];

right before doing the heavy processing operations, and after completing them respectively.



回答4:

Just make sure the overlay view and all of its parent views do not have userInteractionEnabled set to NO, are not hidden, and do not have an alpha less than 0.01. Then the user's touches will interact with the overlay instead of the views visible behind it. Also be aware that it will not block interaction in an area that is not covered by its superviews: even if your view covers the whole screen, if its parent is just 10x10 then it will only block interaction within that 10x10 area covered by its parent.

You may also need to ensure that resignFirstResponder is called on any existing first responder, or the user will still be able to interact with it via the keyboard.