Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Hide keyboard when user touches uiview

Tags:

iphone

I have a UIView with multiple text boxes. Now i have used delegates to change the responder from from text field to another. In this case my key board goes away when the user comes to last text field.

but now i want to hide my key board when the user touches UIView(touches any where on screen apart from text boxes). Can some one help me with this.

Thanks

like image 665
nbojja Avatar asked Jun 08 '09 05:06

nbojja


2 Answers

Use resignFirstResponder in touchesBegan, like so:

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {     UITouch * touch = [touches anyObject];     if(touch.phase == UITouchPhaseBegan) {         [self.myTextField resignFirstResponder];     } } 

You may need to call this on multiple text fields if you're not sure where the focus is currently located; I haven't tested that case.

Additionally, in Interface Builder, you'll need to enable the "User Interaction Enabled" checkbox for the view, or programatically use:

myView.userInteractionEnabled = YES; 
like image 120
PCheese Avatar answered Sep 29 '22 15:09

PCheese


Just call this in your view controller when you want to hide the keyboard.

[self.view endEditing:NO];

like image 28
charliehorse55 Avatar answered Sep 29 '22 14:09

charliehorse55