Is there any way to make a UIView resign its first responder status when the user taps outside of the view bounds?
4 Answers
Capture the touch in the other view or views. When handling that touch, call a selector that has your view resign its responder status.

- 95,983
- 54
- 240
- 345
Found better answer when doing research...
Get the current first responder without using a private API
there is sample code on how to navigate the views to find the first-responder which could be used as the basis of you solution
You could place a transparent view first, then your view as a sub view. Then any touch events in the transparent view could be used to resign first responder.
This might be an approach if there are multiple views, outside the boundary of the primary view to be managed

- 1
- 1

- 33,180
- 5
- 60
- 80
This is quite straight forward and needs to be covered in two steps:
- Add a gesture recogniser to catch the view tap
- Resign the first responder
Thanks to @Nathan Eror for the first part. We can add a gesture recogniser to the viewDidLoad method to register when the user taps the screen:
UITapGestureRecognizer *singleFingerTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleSingleTap:)];
[self.view addGestureRecognizer:singleFingerTap];
Next we will add the function to detect this and the code to remove the keyboard:
- (void)handleSingleTap:(UITapGestureRecognizer *)recognizer {
CGPoint location = [recognizer locationInView:[recognizer.view superview]];
[textField resignFirstResponder];
}
It is worth noting that if you have multiple textFields in your view you will need to resign them all as there is no way of the touch knowing which is the current first responder.
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{
[yourtextfield resignFirstResponder];
//You can have multiple textfields in here
}

- 2,083
- 4
- 29
- 34

- 412
- 5
- 14
-
3Could you please explain your answer at least roughly? – Robin Ellerkmann Jan 09 '15 at 14:40
-
when user touch on screen this delegate method will call as this is viewcontroller class which is subclass of UIViewController class.So on the call of this method we are calling the UITextField delegate method "resignFirstResponder" . – Sandeep Jangir Jan 10 '15 at 12:05