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

 

 
posted @ 2013-11-18 15:56  疯狂の小石子  阅读(13206)  评论(0编辑  收藏  举报