bandw
bandw

Reputation: 889

Detecting touch event outside certain 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?

Upvotes: 4

Views: 8617

Answers (1)

Lucas Eduardo
Lucas Eduardo

Reputation: 11675

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
}

Upvotes: 8

Related Questions