Change UISearchBar/Keyboard Search Button Title
In the UISearchBar control, is the开发者_JS百科 a way to change the Search key title for the keyboard to Done?
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
}
}
}
At least for iOS 8, simply:
[self.searchBar setReturnKeyType:UIReturnKeyDone];
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];
}
}
}
}
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;
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
}
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()
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
.
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];
}
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];
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;
}
}
精彩评论