Change UISearchBar/Keyboard Search Button Title

2019-01-12 23:19发布

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

10条回答
别忘想泡老子
2楼-- · 2019-01-12 23:49

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.

查看更多
欢心
3楼-- · 2019-01-12 23:49

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;
    }
}
查看更多
对你真心纯属浪费
4楼-- · 2019-01-12 23:50

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

}
查看更多
趁早两清
5楼-- · 2019-01-12 23:53

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];
            }
        }      
    }
}
查看更多
成全新的幸福
6楼-- · 2019-01-12 23:59

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
                }
            }
        }
查看更多
叛逆
7楼-- · 2019-01-12 23:59

At least for iOS 8, simply:

    [self.searchBar setReturnKeyType:UIReturnKeyDone];
查看更多
登录 后发表回答