Reputation: 1590
How can I change the return key on the iPhone while it is editing. I know about
myTextField.returnKeyType = UIReturnKeySearch;
However, this only works if I call [myTextField resignFirstResponder];
then [myTextField becomeFirstResponder];
which hides the keyboard then brings it back. Otherwise, it just stays as the one I had before, in this case it was "Go". I need to keep switching them depending on the characters the user enters.
In one line: How do I change the return key type of the keyboard while it is still editing?
Upvotes: 30
Views: 9365
Reputation: 13527
Swift 3
After changing returnKeyType
of your UITextField
or UITextView
call:
textFieldOrView.resignFirstResponder()
textFieldOrView.becomeFirstResponder()
Although calling textFieldOrView.reloadInputViews()
works in some cases, it's not as safe as the solution above.
Upvotes: 0
Reputation: 11
I needed to update the returnKeyType (Next <--> Go) based on the current content of a textfield. The reloadInputViews method on the textfield did not work after setting the returnKeyType, alone. As other posters have mentioned, a resignFirstResponder followed by a becomeFirstResponder was necessary. In my situation, when the textfield became the firstResponder and displayed the keyboard, it would animate the dismissal and reappearance of the keyboard. On smaller devices or in landscape mode, it would also cause the view containing the textfield to jump momentarily. A solution that I have found to hide this unwanted behavior is to place the resignFirstResponder and becomeFirstResponder in a 0 second duration animation block.
[UIView animateWithDuration:0.0
delay:0.0
options:0
animations:^{
[textField reloadInputViews];
[textField resignFirstResponder];
[textField becomeFirstResponder];
}
completion:nil];
Upvotes: 1
Reputation: 2971
I did it with those 3 lines of code:
[sender setReturnKeyType:UIReturnKeyDone];
[sender resignFirstResponder];
[sender becomeFirstResponder];
Upvotes: 1
Reputation: 9649
In my case what it worked was the following:
sender.returnKeyType=UIReturnKeyNext;
[sender reloadInputViews];
[sender resignFirstResponder];
[sender becomeFirstResponder];
I had to force risignFirstResponder followed by a becomeFirstResponder.
Upvotes: 5
Reputation: 735
I made a custom Category to expose returnKeyType.
Just import and do self.addField.returnKeyType = UIReturnKeySearch;
or whatever you like to set as returnKeyType. You can also use it dynamically when the keyboard is already open. It will automatically refresh.
Here it is:
Interface:
/*
Simple hack for exposing returnKeyType for UISearchBar without private APIs
For you from CodeBuffet ;)
Enjoy!
~PW
*/
#import <UIKit/UIKit.h>
@interface UISearchBar (ReturnType)
@property (nonatomic, readwrite) UIReturnKeyType returnKeyType;
@end
Implementation:
#import "UISearchBar+ReturnType.h"
@implementation UISearchBar (ReturnType)
UITextField *textField;
- (UITextField *) traverseForTextViewInViews: (NSArray*) views
{
// Traverse over all views recursively until we find the tresure TextField hidden deep the UISearchBar ocean!
for(UIView *subView in views) {
if([subView conformsToProtocol:@protocol(UITextInputTraits)]) {
return (UITextField *) subView;
}
UITextField *tv = [self traverseForTextViewInViews:subView.subviews];
if (tv) {
return tv;
}
}
return nil;
}
#pragma mark Custom Setters & Getters
- (void)setReturnKeyType:(UIReturnKeyType)returnKeyType
{
if (!textField) {
textField = [self traverseForTextViewInViews:self.subviews];
}
textField.returnKeyType = returnKeyType;
[self reloadInputViews];
[textField reloadInputViews];
}
- (UIReturnKeyType)returnKeyType
{
if (!textField) {
textField = [self traverseForTextViewInViews:self.subviews];
}
return textField.returnKeyType;
}
@end
Upvotes: 2
Reputation: 1590
I found it a while back, forgot to post back here. I am using:
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:
(NSRange)range replacementString:(NSString *)string {
[self performSelector:@selector(addressBarTextDidChange:) withObject:nil afterDelay:0.1];
return YES;
}
-(void)addressBarTextDidChange:(NSString *)text {
NSString *addressText = @"..."; //Wherever you get your text from...
if ([addressText isEqualToString:@""]==NO) {
if ([addressText rangeOfString:@" "].location != NSNotFound) {
//[addressBarTextField setReturnKeyType:UIReturnKeySearch];
if (addressBarTextField.returnKeyType!=UIReturnKeySearch) {
[UIView beginAnimations:nil context:NULL]; //Setup the animation.
[UIView setAnimationDuration:0.0]; //The duration for the animation.
[addressBarTextField resignFirstResponder];
addressBarTextField.returnKeyType = UIReturnKeySearch;
[addressBarTextField becomeFirstResponder];
[UIView commitAnimations];
}
} else {
//[addressBarTextField setReturnKeyType:UIReturnKeyGo];
if (addressBarTextField.returnKeyType!=UIReturnKeyGo) {
[UIView beginAnimations:nil context:NULL]; //Setup the animation.
[UIView setAnimationDuration:0.0]; //The duration for the animation.
[addressBarTextField resignFirstResponder];
addressBarTextField.returnKeyType = UIReturnKeyGo;
[addressBarTextField becomeFirstResponder];
[UIView commitAnimations];
}
}
} else {
if (addressBarTextField.returnKeyType!=UIReturnKeyDone) {
[UIView beginAnimations:nil context:NULL]; //Setup the animation.
[UIView setAnimationDuration:0.0]; //The duration for the animation.
[addressBarTextField resignFirstResponder];
addressBarTextField.returnKeyType = UIReturnKeyDone;
[addressBarTextField becomeFirstResponder];
[UIView commitAnimations];
}
}
}
Basically, I am animating the keyboard in and out for a duration of 0.0 seconds (so the user won't see it). I am also checking if the keyboard is not already the one I want before switching because constantly switching causes lag.
Upvotes: 6
Reputation: 3481
Did you try to use TextField's delegate:
-(BOOL)textFieldShouldBeginEditing:(UITextField *)textField { textField.returnKeyType = UIReturnKeySearch; return YES; }
Upvotes: 0