Change UISearchBar/Keyboard Search Button Title

IphoneButtonUisearchbarTitle

Iphone Problem Overview


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

Iphone Solutions


Solution 1 - Iphone

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

Solution 2 - Iphone

At least for iOS 8, simply:

    [self.searchBar setReturnKeyType:UIReturnKeyDone];

Solution 3 - Iphone

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

Solution 4 - Iphone

One more useful hint, to the Run Loop code (in "@try") section.

This enabled "Done" button when text field is empty:

UITextField *tf = (UITextField *)searchBarSubview;
tf.enablesReturnKeyAutomatically = NO;

Solution 5 - Iphone

For Swift to change return key of UISearchBar

searchBar.returnKeyType = UIReturnKeyType.done

enum available are as below

public enum UIReturnKeyType : Int {
    
    case default
    case go
    case google
    case join
    case next
    case route
    case search
    case send
    case yahoo
    case done
    case emergencyCall
    @available(iOS 9.0, *)
    case continue
}

Solution 6 - Iphone

Just a reminder! If you searchBar stays as first responder, after you change your returnKeyType, you need to dismiss your keyboard and pop it up again to see the changes.

search.resignFirstResponder()
searchBar.returnKeyType = UIReturnKeyType.Done
search.becomeFirstResponder()

Solution 7 - Iphone

As it is a protocol with optional methods, you should test each method separately instead of try-catching.

for (UIView *searchBarSubview in searchBar.subviews)
{
    if ([searchBarSubview conformsToProtocol:@protocol(UITextInputTraits)])
    {
        // keyboard appearance
        if ([searchBarSubview respondsToSelector:@selector(setKeyboardAppearance:)])
            [(id<UITextInputTraits>)searchBarSubview setKeyboardAppearance:UIKeyboardAppearanceAlert];
        // return key 
        if ([searchBarSubview respondsToSelector:@selector(setReturnKeyType:)])
            [(id<UITextInputTraits>)searchBarSubview setReturnKeyType:UIReturnKeyDone];
        // return key disabled when empty text
        if ([searchBarSubview respondsToSelector:@selector(setEnablesReturnKeyAutomatically:)])
            [(id<UITextInputTraits>)searchBarSubview setEnablesReturnKeyAutomatically:NO];
        // breaking the loop when we are done
        break;
    }
}

This will work for iOS <= 6. For iOS >= 7, you need to loop in searchBar.subviews[0].subviews.

Solution 8 - Iphone

Since the Alert-style keyboards are semi-transparent, I can see my view behind it. It doesn't look very good since I have multiple elements behind the keyboard that makes it hard for the keys to stand out. I wanted an all-black keyboard.

So I animated a black UIImageView into position behind the keyboard when text is edited. This gives the appearance of an all-black keyboard.

- (void)textFieldDidBeginEditing:(UITextField *)textField {
	
	[UIView beginAnimations:nil context:nil];
	[UIView setAnimationDuration:0.25]; 
		
	blackBoxForKeyboard.frame = CGRectMake(0, 377, 320, 216);
	[UIView commitAnimations]; 

}

Solution 9 - Iphone

I tried all the solutions shown here, and none of them worked for my UISearchBar (xcode5 compiling for iOS7). I ended up with this recursive function which worked for me:

- (void)fixSearchBarKeyboard:(UIView*)searchBarOrSubView {
    
    if([searchBarOrSubView conformsToProtocol:@protocol(UITextInputTraits)]) {
        if ([searchBarOrSubView respondsToSelector:@selector(setKeyboardAppearance:)])
            [(id<UITextInputTraits>)searchBarOrSubView setKeyboardAppearance:UIKeyboardAppearanceAlert];
        if ([searchBarOrSubView respondsToSelector:@selector(setReturnKeyType:)])
            [(id<UITextInputTraits>)searchBarOrSubView setReturnKeyType:UIReturnKeyDone];
        if ([searchBarOrSubView respondsToSelector:@selector(setEnablesReturnKeyAutomatically:)])
            [(id<UITextInputTraits>)searchBarOrSubView setEnablesReturnKeyAutomatically:NO];
    }
    
    for(UIView *subView in [searchBarOrSubView subviews]) {
        [self fixSearchBarKeyboard:subView];
    }
}

I then called it like so:

_searchBar = [[UISearchBar alloc] init];
[self fixSearchBarKeyboard:_searchBar];
    

Solution 10 - Iphone

Just for covering all the iOS versions:

NSArray *subviews = [[[UIDevice currentDevice] systemVersion] floatValue] < 7 ? _searchBar.subviews : _searchBar.subviews[0].subviews;

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

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionJim BView Question on Stackoverflow
Solution 1 - IphoneRunLoopView Answer on Stackoverflow
Solution 2 - IphonepickwickView Answer on Stackoverflow
Solution 3 - IphoneGregory Cosmo HaunView Answer on Stackoverflow
Solution 4 - IphonetheWalkerView Answer on Stackoverflow
Solution 5 - IphoneHardik ThakkarView Answer on Stackoverflow
Solution 6 - IphoneCodingpanView Answer on Stackoverflow
Solution 7 - IphoneCœurView Answer on Stackoverflow
Solution 8 - IphoneRyeMAC3View Answer on Stackoverflow
Solution 9 - IphonedacoinminsterView Answer on Stackoverflow
Solution 10 - IphoneZarakiView Answer on Stackoverflow