Andy Barnard Andy Barnard - 1 month ago 17
iOS Question

UISearchBar Keyboard Return Key

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'.

Neo Neo
Answer

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".