The following will hide a keyboard nicely;
// Hides a keyboard by use of an invisible button on view
// configure UIButton - Custom, no title & size over touch area
-(IBAction)hideButton:(UIButton *)sender
{
NSLog(@"Hides the keyboard");
[self.view endEditing:YES];
}
Here's another way that's less of a cludge;
It sets up a gesture for the view
1- Set the .h file to <UIGestureRecognizerDelegate>
2 - Add this to the viewDidLoad to set up the gestureRecognizer
// Tap to hide keyboard
UITapGestureRecognizer *hideKeyboardTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(hideTap:)];
[self.view addGestureRecognizer:hideKeyboardTap];
Probably the BEST way;
1 - Don't forget to set the class as a UITextFieldDelegate
2- Add to viewDidLoad for all textFields - self.textField.delegate = self;
3 - And finally the delegate method
- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
[textField resignFirstResponder];
return YES;
}
3 - Add this method
- (void)hideTap:(UIGestureRecognizer *)gestureRecognizer
{
[self.view endEditing:YES];
NSLog(@"Hides the keyboard");
}