再正式开始之前,先来介绍一下IOS的键盘类型:

一、键盘风格

UIKit框架支持8种风格键盘

?
1
2
3
4
5
6
7
8
9
10
typedef enum
    UIKeyboardTypeDefault,                // 默认键盘:支持所有字符  
    UIKeyboardTypeASCIICapable,           // 支持ASCII的默认键盘  
    UIKeyboardTypeNumbersAndPunctuation,  // 标准电话键盘,支持+*#等符号  
    UIKeyboardTypeURL,                    // URL键盘,有.com按钮;只支持URL字符  
    UIKeyboardTypeNumberPad,              //数字键盘  
    UIKeyboardTypePhonePad,               // 电话键盘  
    UIKeyboardTypeNamePhonePad,           // 电话键盘,也支持输入人名字  
    UIKeyboardTypeEmailAddress,           // 用于输入电子邮件地址的键盘  
} UIKeyboardType;

用法用例:

textView.keyboardtype = UIKeyboardTypeNumberPad;

二、键盘外观

  1. ?
    1
    2
    3
    4
    typedef enum
        UIKeyboardAppearanceDefault,    // 默认外观:浅灰色  
        UIKeyboardAppearanceAlert,      //深灰/石墨色  
    } UIKeyboardAppearance;

    用法用例:

    textView.keyboardAppearance=UIKeyboardAppearanceDefault;

    三、回车键

    1. ?
      1
      2
      3
      4
      5
      6
      7
      8
      9
      10
      11
      12
      13
      typedef enum
          UIReturnKeyDefault,  //默认:灰色按钮,标有Return
          UIReturnKeyGo,  //标有Go的蓝色按钮
          UIReturnKeyGoogle,  //标有Google的蓝色按钮,用于搜索
          UIReturnKeyJoin,  //标有Join的蓝色按钮
          UIReturnKeyNext,  //标有Next的蓝色按钮
          UIReturnKeyRoute,  //标有Route的蓝色按钮
          UIReturnKeySearch,  //标有Search的蓝色按钮
          UIReturnKeySend,  //标有Send的蓝色按钮
          UIReturnKeyYahoo,  //标有Yahoo!的蓝色按钮,用于搜索
          UIReturnKeyDone,  //标有Done的蓝色按钮
          UIReturnKeyEmergencyCall,  //紧急呼叫按钮
      } UIReturnKeyType;

      用法用例:

      textView.returnKeyType=UIReturnKeyGo;

      四、自动大写

      1. ?
        1
        2
        3
        4
        5
        6
        typedef enum
            UITextAutocapitalizationTypeNone, //不自动大写  
            UITextAutocapitalizationTypeWords, //单词首字母大写  
            UITextAutocapitalizationTypeSentences, //句子首字母大写  
            UITextAutocapitalizationTypeAllCharacters, //所有字母大写  
        } UITextAutocapitalizationType;

        用法用例:

        textField.autocapitalizationType = UITextAutocapitalizationTypeWords;

        五、自动更正

        1. ?
          1
          2
          3
          4
          5
          typedef enum
              UITextAutocorrectionTypeDefault,//默认  
              UITextAutocorrectionTypeNo,//不自动更正  
              UITextAutocorrectionTypeYes,//自动更正  
          } UITextAutocorrectionType;

          用法用例:

          textField.autocorrectionType = UITextAutocorrectionTypeYes;

          六、安全文本输入

          textView.secureTextEntry=YES;

          开启安全输入主要是用于密码或一些私人数据的输入,此时会禁用自动更正和自此缓存。

          以上内容都可以在 inspector中设置:

          在 iOS 程序中当想要在文本框中输入数据,轻触文本框会打开键盘。对于 iPad 程序,其键盘有一个按钮可以用来关闭键盘,但是 iPhone 程序中的键盘却没有这样的按钮,不过我们可以采取一些方法关闭它。例如,我们可以实现按下 Rerun (有时也是 Done、Research 等)键关闭键盘,或者,更人性化的,轻触背景关闭键盘。

          1、首先讲一下按下Return键关闭键盘。

          当按下键盘的 Return 键,会产生一个 Did End On Exit 事件,此时,我们告诉文本框要放弃控件,于是键盘就消失了。

          假设,我们已经创建了一个 Single View Application ,并打开 ViewController.xib 文件,在 View 上拖上去了三个 Text Field ,然后,我们把这三个文本框映射到 ViewController.h 中,名称依次是 firstField、secondField 以及 thirdField 。如下图:

          (1)在 ViewController.h 中声明一个方法:

          ?
          1
          - (IBAction)textFiledReturnEditing:(id)sender;

          (2)在 ViewController.m 中实现这个方法:

          ?
          1
          2
          3
          -(IBAction)textFiledReturnEditing:(id)sender {
              [sender resignFirstResponder];
          }

          让这三个文本框都映射到 textFiledReturnEditing 方法,不过此时的事件应当是 Did End On Exit ,具体操作是:

          打开 Assistant Editor ,左边打开 ViewController.xib ,右边打开 ViewController.h ,在 Xcode 最右边打开 Connector Inspector ,然后在 View 中选择第一个文本框,在 Connector Inspector 中找到 Did End On Exit ,从它右边的圆圈中拉出映射线,映射到 ViewController.h 的 textFiledReturnEditing 方法,如下图:

          给其他两个文本框进行同样的操作。现在,已经实现了轻触 Return 键关闭键盘。

          2、下面介绍轻触背景关闭键盘。

          (1)在 ViewController.h 文件中添加方法声明代码:

          ?
          1
          - (IBAction)backgroundTap:(id)sender;

          (2)在ViewController.m中实现这个方法:

          ?
          1
          2
          3
          4
          5
          - (IBAction)backgroundTap:(id)sender {
              [firstField resignFirstResponder];
              [secondField resignFirstResponder];
              [thirdField resignFirstResponder];
          }

          (3)让 View 映射到这个方法,不过事先,我们先要改变 View 的类型。

          打开xib,选中 View ,打开 Identity Inspector ,在 class 中选择 UIControl :

          4)打开Assistant Editor ,左边打开 ViewController.xib ,右边打开 ViewController.h ,在Xcode最右边打开 Connector Inspector ,在 ViewController.xib 中选择 Control ,在 Connector Inspector 中找到 Touch Down ,从它右边的圆圈中拉出映射线,映射到 ViewController.h 的 backgroundTap 方法,如下图:

          运行结果:

          打开键盘之后,在背景区域点击一下,键盘就会向下收起来。

          三.解决虚拟键盘挡住UITextField的方法

          因为屏幕太小的缘故,一个键盘跳出来总是把输入框挡住,所以需要移动屏幕来匹配键盘

          ?
          1
          2
          3
          4
          5
          6
          7
          8
          9
          10
          11
          12
          13
          14
          15
          16
          17
          18
          19
          20
          21
          22
          23
          24
          25
          26
          27
          28
          29
          30
          31
          32
          33
          34
          35
          36
          37
          38
          39
          40
          41
          42
          43
          44
          45
          46
          47
          #pragma mark -
          #pragma mark 解决虚拟键盘挡住UITextField的方法
          - (void)keyboardWillShow:(NSNotification *)noti
          {      
              //键盘输入的界面调整      
              //键盘的高度
              float height = 216.0;              
              CGRect frame = self.view.frame;      
              frame.size = CGSizeMake(frame.size.width, frame.size.height - height);      
              [UIView beginAnimations:@"Curl"context:nil];//动画开始        
              [UIView setAnimationDuration:0.30];         
              [UIView setAnimationDelegate:self];        
              [self.view setFrame:frame];       
              [UIView commitAnimations];
          }
          -(BOOL)textFieldShouldReturn:(UITextField *)textField
          {      
              // When the user presses return, take focus away from the text field so that the keyboard is dismissed.      
              NSTimeInterval animationDuration = 0.30f;      
              [UIView beginAnimations:@"ResizeForKeyboard" context:nil];      
              [UIView setAnimationDuration:animationDuration];      
              CGRect rect = CGRectMake(0.0f, 0.0f, self.view.frame.size.width, self.view.frame.size.height); 
              //CGRect rect = CGRectMake(0.0f, 20.0f, self.view.frame.size.width, self.view.frame.size.height);
              self.view.frame = rect;
              [UIView commitAnimations];
              [textField resignFirstResponder];
              return YES;      
          }
          - (void)textFieldDidBeginEditing:(UITextField *)textField
          {      
              CGRect frame = textField.frame;
              int offset = frame.origin.y + 32 - (self.view.frame.size.height - 216.0);//键盘高度216
              NSTimeInterval animationDuration = 0.30f;              
              [UIView beginAnimations:@"ResizeForKeyBoard" context:nil];              
              [UIView setAnimationDuration:animationDuration];
              float width = self.view.frame.size.width;              
              float height = self.view.frame.size.height;      
              if(offset > 0)
              {
                  CGRect rect = CGRectMake(0.0f, -offset,width,height);              
                  self.view.frame = rect;      
              }      
              [UIView commitAnimations];              
          }
          #pragma mark -

          只要在代码中加入这三个文件,然后将自身delegate

          控制器添加UITextFieldDelegate

          ?
          1
          @interface ViewController : UIViewController<uitextfielddelegate></uitextfielddelegate>

          在viewDidLoad中添加:

          ?
          1
          2
          3
          4
          5
          6
          7
          8
          9
          - (void)viewDidLoad
          {
              [super viewDidLoad];
              self.firstTextField.delegate=self;
               
              self.secondTextField.delegate=self;
              self.thirdTextField.delegate=self;
               
          }

          但是这里经常会有屏幕移动后不能返回的问题,这里的解决方案就是

          ?
          1
          2
          3
          4
          5
          6
          7
          8
          9
          10
          11
          - (IBAction)backgroundTap:(id)sender {
              [self.firstTextField resignFirstResponder];
              [self.secondTextField resignFirstResponder];
              [self.thirdTextField resignFirstResponder];
              NSTimeInterval animationDuration = 0.30f;
              [UIView beginAnimations:@"ResizeForKeyboard" context:nil];
              [UIView setAnimationDuration:animationDuration];
              CGRect rect = CGRectMake(0.0f, 0.0f, self.view.frame.size.width, self.view.frame.size.height);
              self.view.frame = rect;
               
          }

          在backgroundTap函数中添加这些代码,这样屏幕就会返回正常了。 
          运行结果:

          下载链接http://download.csdn.net/detail/superlele123/6986691

转载于:https://www.cnblogs.com/OIMM/p/4799956.html

隐藏虚拟键盘,解决键盘挡住UITextField问题相关推荐

  1. IOS 关闭键盘 退出键盘 的5种方式

    1.点击编辑区以外的地方(UIView) 2.点击编辑区域以外的地方(UIControl) 3.使用制作收起键盘的按钮 4.使用判断输入字元 5.关于键盘遮蔽的问题 1,点击编辑区以外的地方(UIVi ...

  2. 全局异步和主线程异步区别、改变PlaceHolder颜色、解决键盘弹起挡住文本框问题...

    1.全局异步执行耗时任务 dispatch_async(dispatch_get_global_queue(0, 0), ^{ }); 2.主线程异步刷新UI dispatch_async(dispa ...

  3. UITextField 文本字段控件 -- IOS (解决键盘遮住View及密文設定的问题)(实例)

    进入本文之前建议你认真读一读我的另一篇博文:UIControl IOS控件编程 这样会起到事半功倍效果. 为什么要看另一篇关于UIControl的文章呢?因为UITextField继承自UIContr ...

  4. 仿QQ空间登录,解决键盘挡住输入框的问题

    我们在开发Android应用中,登录和注册界面是少不了的,往往在做登录注册的时候如果给界面加一个LOGO,就有可能把用户名和密码框放在手机屏幕的中间或底部,这样当软键盘弹出的时候,就有可能挡住输入框( ...

  5. android 无法隐藏键盘,我无法在Android上隐藏虚拟键盘

    我正在使用支持库处理片段和嵌套片段.我无法在Android上隐藏虚拟键盘 我有一个场景,我从现有的片段中添加一个新的片段(其中包含一个EditText).当用户点击EditText时,会显示一个虚拟键 ...

  6. iOS开发中防止键盘挡住UITextField解决方案

    最近转入ios开发,发现ios的UITextField如果在屏幕的最底部的时候,键盘不能自动的调整界面的布局,需要手动的调整位置才可以,所以自己研究和拿着笔话,想写一个通用的方法来实现每一个界面自动适 ...

  7. Android常见公有方法(隐藏虚拟按键/隐藏软键盘/获取屏幕宽高等)

    隐藏虚拟按键,并且全屏 使用: ScreenUtils.hideBottomUIMenu(getWindow().getDecorView()); /*** 隐藏虚拟按键,并且全屏*/public s ...

  8. android 隐藏虚拟键盘,android隐藏全面屏虚拟键盘实现

    下面是使用介绍必须放到setContentView前面 /** * 隐藏虚拟按键,并且设置成全屏 */ protected void hideBottomUIMenu() { if (Build.VE ...

  9. iOS 解决键盘遮挡输入框问题,输入框随键盘弹起上移,切换输入法时动态调整 (Swift)

    最近在项目中需要解决苹果系统输入法遮挡输入框的问题,预期结果为: 若键盘弹出后会遮挡输入框,则输入框随键盘弹起上移,输入法切换时输入框始终保持在距键盘上方 4pt 处; 若键盘弹出后不会遮挡输入框,但 ...

最新文章

  1. 力挺Python!同是程序员,为啥同事年前就实现了财务自由?
  2. Linux 忘记密码如何登陆—续篇2救援模式
  3. mybatis中的MapperAnnotationBuilder
  4. QT5界面操作2:如何用状态栏显示鼠标坐标
  5. mysql数据库的安装
  6. axios 跨域代理
  7. Spark基础学习笔记12:Scala内建控制结构
  8. Multiple substitutions specified in non-positional format; did you mean to add the fo
  9. 全球首发!计算机视觉Polygon Mesh Processing总结7——Remeshing Local Structure
  10. 推荐几个值得关注的爬虫库
  11. 未能加载nStuff.ScriptSharp.Web.dll
  12. 奥威软件大数据bi_哪家BI软件能做Sql server的数据可视化分析?
  13. centos ifconfig 无法使用问题
  14. 无线桥接dhcp服务器不启动,TP-Link路由器桥接提示“获取IP地址失败,请检查DHCP是否开启...
  15. llvm-IR基础知识
  16. Flutter之常用组件的使用举例(中)
  17. 时光穿梭机(撤销修改)
  18. 美国PARKER派克永磁高速伺服电机——GVM电机
  19. linux安装firefox
  20. 视频编解码-----理论基础(3)

热门文章

  1. SQL2000无法建立管理单元
  2. 开发过程中任何一个时刻,只关注解决当前面临的问题。
  3. Gentle.NET 使用文档
  4. 不用GAN、VAE,谷歌发明视频生成的扩散模型,实现新SOTA
  5. 【项目合作】移动端人体姿态估计
  6. 清华大学团队夺冠AAAI 2021国际深度元学习挑战赛
  7. C++ Primer 第五版 第7章类 7.1——类讲解(成员函数、非成员函数、构造函数)
  8. TCP、UDP(网络协议:传输层协议)
  9. 给python小白的几个小练习(附答案详解哦)
  10. mvp关联activity生命周期_Android MVP架构从入门到精通-真枪实弹