Question

How to change RETURN type of the keyboard when UISearchBar is first responder? For example I want to put DONE instead of SEARCH. I couldn't select it from XIB attribute which I normally did on UITextField.

Was it helpful?

Solution

There is no direct access so you need to find the searchbars UITextField and change it's keyboard.

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;
        }
    }
}

OTHER TIPS

let say you have a searchbar named as srchbar // Set the return key and keyboard appearance of the search bar

 for (UIView *searchBarSubview in [srchbar subviews]) {

        if ([searchBarSubview conformsToProtocol:@protocol(UITextInputTraits)]) {

            @try {

                [(UITextField *)searchBarSubview setReturnKeyType:UIReturnKeyDone];
                [(UITextField *)searchBarSubview setKeyboardAppearance:UIKeyboardAppearanceAlert];
            }
            @catch (NSException * e) {

                // ignore exception
            }
        }
    }
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top