How to hide the keyboard when I press return key in a UITextField?
First make your file delegate for UITextField
@interface MYLoginViewController () <UITextFieldDelegate>@end
Then add this method to your code.
- (BOOL)textFieldShouldReturn:(UITextField *)textField { [textField resignFirstResponder]; return YES;}
Also add self.textField.delegate = self;
In viewDidLoad
declare:
[yourTextField setDelegate:self];
Then, include the override of the delegate method:
-(BOOL)textFieldShouldReturn:(UITextField *)textField{ [textField resignFirstResponder]; return YES;}
Try this in Swift,
Step 1: Set delegate as self to your textField
textField.delegate = self
Step 2: Add this UITextFieldDelegate below your class declaration,
extension YourClassName: UITextFieldDelegate { func textFieldShouldReturn(textField: UITextField) -> Bool { textField.resignFirstResponder() return true }}