Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

UISearchBar Keyboard Return Key

Tags:

ios

iphone

I am using a UISearchBar to match text input against entries in a database and display the matched results to the user in a UITableView, as they type.

All is well, however, I cannot find a way to alter the return key type of the search bar's keyboard. By default it replaces the standard return key with a Search button. Because I am doing a live search as the user types, I do not need this button and having it there and inactive has raised some usability issues.

Attempted solutions

  1. I can set a keyboard with the setKeyboard:UIKeyboardType method, however this doesn't seem to override the default setting of replacing the return key (on the standard keyboard) with a Search key and it does not allow access to change this return key.

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

Is there a way that I can keep the functionality of the search bar's delegate methods, whilst having legitimate access to the keyboard's return key?

In fact, almost the exact screen I am implementing is found in Apple's Clock application
Screenshot:
enter image description here

So any help on a clean solution would be much appreciated. Note the return key on the bottom right instead of the default Search button'.

like image 602
Andy Barnard Avatar asked Dec 20 '11 23:12

Andy Barnard


4 Answers

Slightly different in iOS 7 compared to the answer of @sudip.

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;
        }
    }
}
like image 119
Martin Stolz Avatar answered Oct 20 '22 21:10

Martin Stolz


I tried all of these solutions without luck until I realized that in IOS8, you can just set searchBar.returnKey = .Done or whatever UIReturnKeyType you like. Sigh.

like image 24
kellanburket Avatar answered Oct 20 '22 19:10

kellanburket


Try this:

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

If you want to dismiss the return key (i.e., make it do nothing), set the "returnKeyType" property on the UITextField subview to "UIReturnKeyDone" along with "keyboardAppearence".

like image 21
Neo Avatar answered Oct 20 '22 19:10

Neo


I had to add some lines to Neo's answer. Here is my code to add a "Done" button for 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;
    }
}
like image 27
sudip Avatar answered Oct 20 '22 21:10

sudip