Remove view when tapped outside

Related searches

I have a UIView, that I have appear when a button is tapped, I am using it as a custom alert view essentially. Now when the user taps outside the custom UIView that I added to the main view, I want to hide the cusomt view, I can easily do this with customView.hidden = YES; but how can I check for the tap outside the view?

Thanks for the help

There are 2 approaches:

First approach:

You can set a tag for your custom view:


An then in your viewcontroller, use the touchesBegan:withEvent: delegate

-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{

    UITouch *touch = [touches anyObject];


Second approach:

It's more likely that every time you want to popup a custom view, there's an overlay behind it, which will fill your screen (e.g. a black view with alpha ~0.4). In these cases, you can add an UITapGestureRecognizer to it, and add it to your view every time you want your custom view to show up. Here's an example:

UIView *overlay;

        overlay = [[UIView alloc] initWithFrame:CGRectMake(0,  0,self.view.frame.size.width, self.view.frame.size.height)];
    [overlay setBackgroundColor:[UIColor colorWithRed:0 green:0 blue:0 alpha:0.5]];

    UITapGestureRecognizer *overlayTap =
    [[UITapGestureRecognizer alloc] initWithTarget:self

    [overlay addGestureRecognizer:overlayTap];

    [self.view addSubview:overlay];


- (void)onOverlayTapped
    NSLog(@"Overlay tapped");
    //Animate the hide effect, you can also simply use customview.hidden=YES;
    [UIView animateWithDuration:0.2f animations:^{

    }completion:^(BOOL finished) {
        [overlay removeFromSuperview];



dismiss keyboard when tap outside swift Code Example, //Uncomment the line below if you want the tap not not interfere and cancel other interactions. 8. //tap.cancelsTouchesInView = false. 9. . 10. view. initially, the self.helpView.isHidden is set to true in viewDidLoad method to hide the view. Now, how can i dismiss this view by touching anywhere outside the view. From the research, i found that, it can be done by creating a transparent button that fits the whole viewController. So then by clicking on the button, we can make the view to dismiss.

Like in the answer of FlySoFast, I tried first approach and it worked I just shared to swift version of it. You can tag it of your custom view and the check the that view touched or not so we achieved our solution I guess.In the below I assign tag value of my custom view to 900.

customview.tag = 900

override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
    let touch = touches.first!
    if touch.view?.tag != 900 {


I hope this answer will help to you

How can I dismiss my slide menu while tapping outside?, Tapping outside should dismiss the menu. SOLUTION 1 : Assign tap gesture delegate to your view controller. In the gesture method keep code� As the name suggests, it can pick up on outside signals and taps, letting you know that your suspicions are correct and that someone else has been listening to your calls. The usefulness of these devices is in question, but in order for any such device to be of any use in detecting a wiretap, it will need to be able to detect electrical or

When you presenting custom alert view, add that custom alert view in to another full screen view, make that view clear by setting its backgroundColor clear. Add full screen view in main view, and add tapGesture in fullScreen invisible view, when ever it gets tap remove this view.

But if you will do this it will dismiss view even when you touch custom alert view for that you need to set delegate of tapGesture and implement this method

- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch
    if ([touch.view isDescendantOfView:self.customAlertView])
        return NO;
    return YES;

Dismiss(Hide) keyboard on tap outside of TextInput(React Native , import { View, TextInput, StyleSheet, Keyboard, TouchableWithoutFeedback } from 'react-native';. Step 2: Create an arrow function to write� <Image Source="tapped.jpg"> <Image.GestureRecognizers> <TapGestureRecognizer Command="{Binding TapCommand}" CommandParameter="Image1" /> </Image.GestureRecognizers> </Image> The complete code for this view model can be found in the sample. The relevant Command implementation details are shown below:

with using function pointInside in Swift:

override func pointInside(point: CGPoint, withEvent event: UIEvent?) -> Bool {

    if let view = customView {
        //if UIView is open open
        let newPoint = self.convertPoint(point, toView: view)
        let pointIsInsideGenius = view.pointInside(newPoint, withEvent: event)
        // tapping inside of UIView
        if pointIsInsideGenius {
            return true
        } else {
        // if tapped outside then remove UIView
                view = nil

    return false

Dismiss/Hide Keyboard by touching Anywhere outside UITextField , Create an extension of the view controller. extension UIViewController {. func dismissKey(). {. let tap: UITapGestureRecognizer =� Barr has tapped outside prosecutor to review case against Flynn: NYT. By Marty Johnson and John Kruzel - 02/14/20 01:45 PM EST .

You can use this library:

Init a PopUpController the view that you want to dismiss it when tap outside

let popup = PopupViewController(contentView: viewNeedToRemoveWhenTapOutside, position: .bottomLeft(position))
present(popup, animated: true, completion: nil)

Showing and hiding views with transitions, that condition changes we can insert or remove views from our view hierarchy. We can get SwiftUI's default view transition by wrapping the state Now tapping the button looks much better: the rectangle scales up as the� I'm working on a project that have a UIViewController, on the view controller there is a UIScrollView and a UITextField on the scrollview. like this: I'm trying to dismiss the keyboard and hide it after typing some text in the textfield and tap anywhere outside the textfield. I've tried the following code:

I need to replace the washer in a tap, however it is difficult to remove the cover. It looks like it is pushed on but it is a flush fit with nowhere to prise it off, as it is domed then it is also difficult to grab it to twist it. I have included photos below, if anybody can help I would

How a guest becomes a member of a team. A team owner or a Microsoft 365 or Office 365 admin adds a guest to a team.; The Microsoft 365 or Office 365 admin or the team owner manages a guest's capabilities as necessary: for example, allowing a guest to add or delete channels or disabling access to files.

A bronze statue of an unnamed Confederate soldier, titled "At Ready," stands outside of the Albemarle County Courthouse on Thursday, Sept. 3, 2020, in Charlottesville, Va.

  • i would suggest the first approach, rather than using an unnecessary control (UITapGestureRecognizer). And most of all " Lesser the code, More stable the app"
  • In this answer first approach is worked on my project I'll share the swift version of it