today I tried to run my code on my iPod (iOS 6.1.3) and I found something interesting here...
first, when I tap on textfield the keyboard shows up but it won't hide when I tap somewhere else outside textfield.
so I decided to Googling and found this solution :
_fieldEmail.delegate = self; _fieldEmail.returnKeyType = UIReturnKeyDone; _fieldPassword.delegate = self; _fieldPassword.returnKeyType = UIReturnKeyDone; _fieldRegisterName.delegate = self; _fieldRegisterName.returnKeyType = UIReturnKeyDone; _fieldRegisterEmail.delegate = self; _fieldRegisterEmail.returnKeyType = UIReturnKeyDone; _fieldRegisterPassword.delegate = self; _fieldRegisterPassword.returnKeyType = UIReturnKeyDone;
it works... it gives a 'DONE' button on the bottom of keyboard and now the keyboard can be hidden by pressing it.
but I have 2 problems here :
that's all I need to know
If you're supporting only iOS 15 and later, you can activate and dismiss the keyboard for a text field by focusing and unfocusing it. In its simplest form, this is done using the @FocusState property wrapper and the focusable() modifier – the first stores a Boolean that tracks whether the second is currently focused.
Android devices have a solution; press the physical back button (provided on some mobile phones) or the soft key back button, and it closes the keyboard.
Via Tap Gesture This is the quickest way to implement keyboard dismissal. Just set a Tap gesture on the main View and hook that gesture with a function which calls view. endEditing . Causes the view (or one of its embedded text fields) to resign the first responder status.
The below code will work on all the components in the UIView for all the UITextField
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event { for (UIView * txt in self.view.subviews){ if ([txt isKindOfClass:[UITextField class]] && [txt isFirstResponder]) { [txt resignFirstResponder]; } } }
OR
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event { [self.view endEditing:YES]; }
resignFirstResponder
In viewDidLoad :
UITapGestureRecognizer * tapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(hideKeyBoard)]; [self.view addGestureRecognizer:tapGesture];
And then :
-(void)hideKeyBoard { [yourTextField resignFirstResponder]; }
2.You could subclass UITextField but unless you have 1000 textFields it is ok to do like you currently do.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With