UISearchBar Keyboard Return Key

I use UISearchBar to match text input with items in the database and display consistent results for the user in the UITableView as they are entered.

Everything is fine, however, I cannot find a way to change the type of return key on the keyboard of the search bar. By default, it replaces the standard return key Search . Since I perform a real-time search as a user, I do not need this button, and the presence there and inactive caused some usability problems.

Attempts to solve

  • I can install the keyboard using the setKeyboard:UIKeyboardType , however this does not seem to override the default setting of replacing the return key (on the standard keyboard) with the Search key, and it does not allow access to change this return key.

  • I thought about using UITextField , giving me access to the returnKeyType property through the UITextInputTraits protocol. My problem is that I implement the UISearchBarDelegate searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText , which I would lose with a UITextField .

Is there a way to preserve the functionality of the search bar delegation methods by having legitimate access to the keyboard backspace key?

In fact, the almost accurate screen that I implement can be found in the Apple Clock app
Screenshot:
enter image description here

So any help on a clean solution would be greatly appreciated. Notice the return key at the bottom right, and not the default Search button.

+8
ios iphone
source share
8 answers

Try the following:

 for (UIView * subView in searchBar.subviews) {
     if ([subView conformsToProtocol: @protocol (UITextInputTraits)]) {
         [(UITextField *) subView setKeyboardAppearance: UIKeyboardAppearanceAlert];
     }
 }

If you want to reject the return key (that is, make it do nothing), set the returnKeyType property in the UITextField subheading to "UIReturnKeyDone" along with "keyboardAppearence".

+11
source share

A bit different in iOS 7 compared to @sudip's answer.

 for (UIView *subview in self.searchBar.subviews) { for (UIView *subSubview in subview.subviews) { if ([subSubview conformsToProtocol:@protocol(UITextInputTraits)]) { UITextField *textField = (UITextField *)subSubview; [textField setKeyboardAppearance: UIKeyboardAppearanceAlert]; textField.returnKeyType = UIReturnKeyDone; break; } } } 
+21
source share

I tried all these solutions with no luck until I realized that in IOS8 you can just set searchBar.returnKey = .Done or any other UIReturnKeyType that you like. Sigh.

+19
source share

I had to add some lines to Neo's answer. Here is my code to add the Finish button for the UISearchbar:

 for(UIView *subView in sb_manSearch.subviews) { if([subView conformsToProtocol:@protocol(UITextInputTraits)]) { UITextField *t = (UITextField *)subView; [t setKeyboardAppearance: UIKeyboardAppearanceAlert]; t.returnKeyType = UIReturnKeyDone; t.delegate = self; break; } } 
+2
source share

You can do it:

 - (void)viewDidLoad { // Adding observer that will tell you keyboard is appeared. [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardDidShow:) name:UIKeyboardDidShowNotification object:nil]; [super viewDidLoad]; } - (void)keyboardDidShow:(NSNotification *)note { keyboardTest = [self getKeyboard]; [keyboardTest setReturnKeyEnabled: YES]; } - (id) getKeyboard // Method that returns appeared keyboard reference { id keyboardView; // locate keyboard view UIWindow* tempWindow = [[[UIApplication sharedApplication] windows] objectAtIndex:1]; UIView* keyboard; for(int i=0; i<[tempWindow.subviews count]; i++) { keyboard = [tempWindow.subviews objectAtIndex:i]; if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 3.2) { if([[keyboard description] hasPrefix:@"<UIPeripheralHost"] == YES) { keyboard = [[keyboard subviews] objectAtIndex:0]; keyboardView = keyboard ; } } else { if([[keyboard description] hasPrefix:@"<UIKeyboard"] == YES) keyboardView = keyboard ; } } return keyboardView ; } 
0
source share

UPDATE: Starting with iOS 7, the accepted answer will not work , the lower version will work with iOS 7 and beyond.

  UIView *subViews = [[_searchBar subviews] firstObject]; for(UIView *subView in [subViews subviews]) { if([subView conformsToProtocol:@protocol(UITextInputTraits)]) { [(UITextField *)subView setEnablesReturnKeyAutomatically:NO]; } } 
0
source share
 for (UIView *subView in view.subviews) { if ([subView isKindOfClass:[UITextField class]]) { UITextField *txt = (UITextField *)subView; @try { [txt setReturnKeyType:UIReturnKeyDone]; [txt setKeyboardAppearance:UIKeyboardAppearanceAlert]; } @catch (NSException * e) { // ignore exception } } } 
0
source share

I just found the simplest expectation to crack it, just put a space when you start editing the search field

 -(void)searchBarTextDidBeginEditing:(UISearchBar *)searchBar{ //Add a blank character to hack search button enable searchBar.text = @" ";} 
-one
source share

All Articles