Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Change UISearchBar/Keyboard Search Button Title

In the UISearchBar control, is the a way to change the Search key title for the keyboard to Done?

like image 354
Jim B Avatar asked Apr 24 '10 19:04

Jim B


3 Answers

For a searchbar named tablesearchbar:

// Set the return key and keyboard appearance of the search bar
        for (UIView *searchBarSubview in [tableSearchBar subviews]) {

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

                @try {

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

                    // ignore exception
                }
            }
        }
like image 193
RunLoop Avatar answered Nov 19 '22 20:11

RunLoop


At least for iOS 8, simply:

    [self.searchBar setReturnKeyType:UIReturnKeyDone];
like image 52
pickwick Avatar answered Nov 19 '22 22:11

pickwick


As of iOS 7 beta 5, Run Loop's answer didn't work for me, but this did:

for(UIView *subView in [searchBar subviews]) {
    if([subView conformsToProtocol:@protocol(UITextInputTraits)]) {
         [(UITextField *)subView setReturnKeyType: UIReturnKeyDone];
    } else {
        for(UIView *subSubView in [subView subviews]) {
            if([subSubView conformsToProtocol:@protocol(UITextInputTraits)]) {
                [(UITextField *)subSubView setReturnKeyType: UIReturnKeyDone];
            }
        }      
    }
}
like image 43
Gregory Cosmo Haun Avatar answered Nov 19 '22 21:11

Gregory Cosmo Haun