Dynamically Changing Keyboard Type for a UISearchBar

后端 未结 3 1152
轻奢々
轻奢々 2020-12-14 20:32

I have an iPhone app that uses a UISearchBar and UISearchDisplayController. The search bar has three scope buttons. I would like the keyboard to

3条回答
  •  甜味超标
    2020-12-14 20:40

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

提交回复
热议问题