• 在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     }
     
  • 相关阅读:
    3.18 每日一练
    第二章 练习
    第一章 练习
    Redis常用操作大全和Python操作Redis
    vue学习【第七篇】:Vue之导入Bootstrap
    Vue学习【第六篇】:Vue-cli脚手架(框架)与实战案例
    Redis 安装,配置以及数据操作
    vue学习【第五篇】:Vue组件
    vue学习【第三篇】:vue之node.js的简单介绍
    Vue学习【第二篇】:ES6简单介绍
  • 原文地址:https://www.cnblogs.com/crazypebble/p/3429633.html
Copyright © 2020-2023  润新知