更改UISearchBar /键盘搜索按钮标题

时间:2010-04-24 19:38:38

标签: iphone button uisearchbar title

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

10 个答案:

答案 0 :(得分:97)

对于名为table searchbar的搜索栏:

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

答案 1 :(得分:48)

至少对于iOS 8,只需:

    [self.searchBar setReturnKeyType:UIReturnKeyDone];

答案 2 :(得分: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];
            }
        }      
    }
}

答案 3 :(得分:18)

另一个有用的提示, Run Loop 代码(在“@try”中)部分。

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

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

答案 4 :(得分:12)

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

答案 5 :(得分:2)

提醒一下!如果您将searchBar保留为第一响应者,则在更改returnKeyType之后,您需要关闭键盘并再次弹出以查看更改。

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

答案 6 :(得分: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&lt; = 6.对于iOS&gt; = 7,您需要循环searchBar.subviews[0].subviews

答案 7 :(得分:0)

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

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

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

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

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

}

答案 8 :(得分: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];

答案 9 :(得分:0)

仅用于涵盖所有iOS版本:

public class MyClass {
  public MyClass() {
    Value = 0.0;
  }

  public Double? Value {
    get;
    set;
  }
}

...

MyClass test = new MyClass();

ApplyNullsForZeroes(test);

if (test.Value == null)
  Console.Write("It's null now");