60

在 UISearchBar 控件中,是否可以将键盘的搜索键标题更改为完成?

4

10 回答 10

99

对于名为 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
                }
            }
        }
于 2010-04-24T20:51:49.237 回答
52

至少对于 iOS 8,简单地说:

    [self.searchBar setReturnKeyType:UIReturnKeyDone];
于 2015-01-28T14:59:31.260 回答
43

从 iOS 7 beta 5 开始,Run Loop 的回答对我不起作用,但确实如此:

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];
            }
        }      
    }
}
于 2013-09-05T00:56:03.183 回答
18

另一个有用的提示,运行循环代码(在“@try”中)部分。

当文本字段为空时,此启用“完成”按钮:

UITextField *tf = (UITextField *)searchBarSubview;
tf.enablesReturnKeyAutomatically = NO;
于 2012-01-23T22:36:07.953 回答
18

让 Swift 改变 UISearchBar 的返回键

searchBar.returnKeyType = UIReturnKeyType.done

可用的枚举如下

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
}
于 2016-05-06T06:51:46.153 回答
3

只是提醒!如果您的 searchBar 仍然是第一响应者,则在您更改 returnKeyType 后,您需要关闭键盘并再次弹出以查看更改。

search.resignFirstResponder()
searchBar.returnKeyType = UIReturnKeyType.Done
search.becomeFirstResponder()
于 2016-09-28T19:08:54.307 回答
1

由于它是一个带有可选方法的协议,因此您应该单独测试每个方法,而不是尝试捕获。

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

这适用于 iOS <= 6。对于 iOS >= 7,您需要循环输入searchBar.subviews[0].subviews.

于 2013-08-26T13:22:02.177 回答
0

由于警报式键盘是半透明的,我可以看到它背后的视图。它看起来不太好,因为我在键盘后面有多个元素,这使得按键很难脱颖而出。我想要一个全黑的键盘。

因此,当编辑文本时,我将黑色 UIImageView 动画到键盘后面的位置。这给出了全黑键盘的外观。

- (void)textFieldDidBeginEditing:(UITextField *)textField {

    [UIView beginAnimations:nil context:nil];
    [UIView setAnimationDuration:0.25]; 

    blackBoxForKeyboard.frame = CGRectMake(0, 377, 320, 216);
    [UIView commitAnimations]; 

}
于 2011-08-12T23:05:10.823 回答
0

我尝试了这里显示的所有解决方案,但没有一个适用于我的 UISearchBar(xcode5 为 iOS7 编译)。我最终得到了这个对我有用的递归函数:

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

然后我这样称呼它:

_searchBar = [[UISearchBar alloc] init];
[self fixSearchBarKeyboard:_searchBar];
于 2014-01-10T01:32:00.680 回答
0

仅用于涵盖所有 iOS 版本:

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;
    }
}
于 2016-02-02T07:18:46.540 回答