Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Detecting touch event outside certain UIView

Tags:

ios

iphone

uiview

In my app, to click a button will pop up a UIView, now I want to click anywhere outside of the UIView to dismiss the UIView.
I have tried adding a large transparent button under the UIView, invoke the button action to dismiss the UIView, but the button can't be expanded to Fullscreen because of the top navigationbar and bottom tabbar
Is any other way to achieve?

like image 485
bandw Avatar asked Feb 15 '23 08:02

bandw


1 Answers

A giant UIButton it's not very good solution to your problem. You can simple use a UIGestureRecognizer for that.

You can allocate one like this:

UITapGestureRecognizer *tapImageRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self  action:@selector(dismissPopUp)];

Then, just add the gesture to the views you want to respond to the selector chosen.

[self.view addGestureRecognizer:tapImageRecognizer];

and possibly others

[self.navBar addGestureRecognizer:tapImageRecognizer];
//etc

Just don't forget to implement the method used by the gesture recognizer

-(void)dismissPopUp
{
   //your dimiss code here
}
like image 137
Lucas Eduardo Avatar answered Feb 24 '23 03:02

Lucas Eduardo