在iOS7中修改键盘Return键的类型

  今天将以前运行在iOS7以前的一段代码拿出来,在iOS7的机器上运行,发现键盘上的ReturnKeyType不能被修改了。
  通过几番查找资料,了解到iOS7中UISearchBar的结构发生了变化,将实现了UITextInputTraits协议的UITextField,又包装了一层UITextField的SubView。所以,枚举UISearchBar获得的子视图,没有实现UITextInputTraits协议,须要对子视图再次进行枚举子视图,才能调用到setReturnKeyType方法。

  这里Mark下,之后写代码可必定要考虑兼容性方面的问题。
 1     // Set Search Button Title to Done
 2     for (UIView *searchBarSubview in [self.searchBar subviews]) {
 3         if ([searchBarSubview conformsToProtocol:@protocol(UITextInputTraits)]) {
 4             // Before iOS 7.0
 5             @try {
 6                 [(UITextField *)searchBarSubview setReturnKeyType:UIReturnKeyDone];
 7                 //[(UITextField *)searchBarSubview setKeyboardAppearance:UIKeyboardAppearanceAlert];
 8             }
 9             @catch (NSException * e) {
10                 // ignore exception
11             }
12         } else {
13             // iOS 7.0
14             for(UIView *subSubView in [searchBarSubview subviews]) {
15                 if([subSubView conformsToProtocol:@protocol(UITextInputTraits)]) {
16                     @try {
17                         [(UITextField *)subSubView setReturnKeyType:UIReturnKeyDone];
18                         //[(UITextField *)searchBarSubview setKeyboardAppearance:UIKeyboardAppearanceAlert];
19                     }
20                     @catch (NSException * e) {
21                         // ignore exception
22                     }
23                 }
24             }
25         }
26     }
相关文章
相关标签/搜索