动态更改UISearchBar的键盘类型

时间:2022-01-20 17:04:25

I have an iPhone app that uses a UISearchBar and UISearchDisplayController. The search bar has three scope buttons. I would like the keyboard to be a numeric keypad when a particular scope button is selected, but be a default keyboard when any other scope button is selected.

我有一个使用UISearchBar和UISearchDisplayController的iPhone应用程序。搜索栏有三个范围按钮。我希望键盘在选择特定范围按钮时成为数字键盘,但在选择任何其他范围按钮时成为默认键盘。

I have implemented the UISearchBarDelegate selectedScopeButtonIndexDidChange:selectedScope method as follows:

我已经实现了UISearchBarDelegate selectedScopeButtonIndexDidChange:selectedScope方法,如下所示:

- (void)searchBar:(UISearchBar *)searchBar selectedScopeButtonIndexDidChange:(NSInteger)selectedScope
{
    switch (selectedScope) {
        case DeviceIDScopeIndex:
            searchBar.keyboardType = UIKeyboardTypeNumberPad;
            break;
        default:
            searchBar.keyboardType = UIKeyboardTypeDefault;
            break;
    }
}

I know that the method gets called, and the right case gets triggered when the button in question is selected. But the onscreen keyboard doesn't change unless I tap the Cancel button to hide the keyboard, then tap the search field again to bring up the keyboard again.

我知道该方法被调用,并且当选择了相关按钮时会触发正确的情况。但屏幕键盘不会改变,除非我点击取消按钮隐藏键盘,然后再次点击搜索字段再次调出键盘。

Is there a way to get the keyboard to change itself while it's onscreen, or to programmatically hide it and then reshow it?

有没有办法让键盘在屏幕上改变自己,或以编程方式隐藏它然后重新显示它?

3 个解决方案

#1


23  

If you're using iOS 3.2 or newer, you can call:

如果您使用的是iOS 3.2或更高版本,可以致电:

[searchBar reloadInputViews]

and it switches immediately without hackery. At least this works for me on a UITextField. (I mention this because the resign/become first responder trick didn't work exactly right for me, but reloadInputViews did.)

并且它立即切换而没有hackery。至少这对UITextField适用。 (我提到这个是因为辞职/成为第一响应者技巧并不适合我,但reloadInputViews确实如此。)

#2


23  

Even though this is kind of a hack, this worked for me:

即使这是一种黑客攻击,这对我有用:

- (void)searchBar:(UISearchBar *)searchBar selectedScopeButtonIndexDidChange:(NSInteger)selectedScope {


    switch (selectedScope) {
    case 0:
       searchBar.keyboardType = UIKeyboardTypeNumberPad;
        break;
   default:
        searchBar.keyboardType = UIKeyboardTypeDefault;
        break;


    // Hack: force ui to reflect changed keyboard type
    [searchBar resignFirstResponder];
    [searchBar becomeFirstResponder];

}

#3


1  

[searchBar reloadInputViews] 

and

[searchBar resignFirstResponder];
[searchBar becomeFirstResponder];

both can work, but the second method have more side effect, change firstResponder, can trigger keyboard's notification, so may affect other logic which depends on the keyboard's notification, and affect the view's frame which depend on keyboard frame's change

两者都可以工作,但第二种方法有更多的副作用,更改firstResponder,可以触发键盘的通知,因此可能影响依赖于键盘通知的其他逻辑,并影响视图的框架,这取决于键盘框架的变化

#1


23  

If you're using iOS 3.2 or newer, you can call:

如果您使用的是iOS 3.2或更高版本,可以致电:

[searchBar reloadInputViews]

and it switches immediately without hackery. At least this works for me on a UITextField. (I mention this because the resign/become first responder trick didn't work exactly right for me, but reloadInputViews did.)

并且它立即切换而没有hackery。至少这对UITextField适用。 (我提到这个是因为辞职/成为第一响应者技巧并不适合我,但reloadInputViews确实如此。)

#2


23  

Even though this is kind of a hack, this worked for me:

即使这是一种黑客攻击,这对我有用:

- (void)searchBar:(UISearchBar *)searchBar selectedScopeButtonIndexDidChange:(NSInteger)selectedScope {


    switch (selectedScope) {
    case 0:
       searchBar.keyboardType = UIKeyboardTypeNumberPad;
        break;
   default:
        searchBar.keyboardType = UIKeyboardTypeDefault;
        break;


    // Hack: force ui to reflect changed keyboard type
    [searchBar resignFirstResponder];
    [searchBar becomeFirstResponder];

}

#3


1  

[searchBar reloadInputViews] 

and

[searchBar resignFirstResponder];
[searchBar becomeFirstResponder];

both can work, but the second method have more side effect, change firstResponder, can trigger keyboard's notification, so may affect other logic which depends on the keyboard's notification, and affect the view's frame which depend on keyboard frame's change

两者都可以工作,但第二种方法有更多的副作用,更改firstResponder,可以触发键盘的通知,因此可能影响依赖于键盘通知的其他逻辑,并影响视图的框架,这取决于键盘框架的变化