Reputation: 1143
In an iOS app, I press a button, then a popup menu shows.
I want the popup menu being closed if I touch anywhere(a UITableView
, other UIButton
) in the screen outside of the menu.
How can I intercept the touch event before the UI element(like UITableView
, UIButton
) on the screen handle the event, so I can close the menu programmatically?
I have tried -(void) touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
in UIViewController
, but it does not work. The UI element I touched will handle the event so I cannot intercept the event.
Upvotes: 0
Views: 1155
Reputation: 130092
Add a transparent UIView
covering your whole screen under the menu. Then handle only the taps to this view.
Upvotes: 3
Reputation: 51
You can add a UIGestureRecognizer
to the main window and intercept the touch. If all you want is a tap then UITapGestureRecognizer
should do the job.
If you are looking for any kind of touch then I would recommend using a custom GestureRecognizer which implements touchesBegan
.
Upvotes: 0
Reputation: 6969
Set userInterActionEnabled
Property of all views to NO
while the menu is visible. Once discarded, set it to YES again.
- (void) enableOrDisableControlsExceptMenu : (UIView *) parentView :(bool) bEnabled
{
for (UIView * view in parentView.subviews)
{
if (![view isKindOfClass:[YourMenu class]) //YourMenu is the menu view class
view.userInterActionEnabled = bEnabled;
[self enableOrDisableControlsExceptMenu : view :bEnabled];
}
}
When the menu is visible, call the above method like:
[self enableOrDisableControlsExceptMenu:self.view :NO];
When the menu is discarded (invisible), call it like:
[self enableOrDisableControlsExceptMenu:self.view :YES];
Upvotes: 0