采用的是通知的常规方式

    // 解决键盘遮挡问题
//选择didShow是因为需要键盘的高度
//选择willHide是因为视图frame重置需要优先于键盘消失,否则表现得不连贯
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(keyboardWasShown:)
name:UIKeyboardDidShowNotification
object:nil]; [[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(keyboardWasHide:)
name:UIKeyboardWillHideNotification
object:nil];

触发的方法

-(void)keyboardWasShown:(NSNotification*)notification
{
//获取键盘高度
NSValue* value=notification.userInfo[UIKeyboardFrameBeginUserInfoKey];
CGFloat keyBoradHeight=[value CGRectValue].size.height;
NSNumber* animationTime=notification.userInfo[UIKeyboardAnimationDurationUserInfoKey];
double time=[animationTime doubleValue];
//获取被遮挡控件距离controller底部的距离
float a=self.view4.frame.origin.y+self.view4.frame.size.height;
float b = self.view.frame.size.height-a;
if (b<keyBoradHeight)
{
//动画效果
[UIView animateWithDuration:time
animations:^
{
//键盘被挡住了
CGRect viewCGrect=self.view.frame;
//视图应该上移所以是-
viewCGrect.origin.y=viewCGrect.origin.y-(keyBoradHeight-b);
[self.view setFrame:viewCGrect]; } completion:nil]; } }
-(void)keyboardWasHide:(NSNotification*)notification
{
NSValue* value=notification.userInfo[UIKeyboardFrameBeginUserInfoKey];
CGFloat keyBoradHeight=[value CGRectValue].size.height; float a=self.view4.frame.origin.y+self.view4.frame.size.height;
float b = self.view.frame.size.height-a; CGRect viewCGrect=self.view.frame;
viewCGrect.origin.y=viewCGrect.origin.y+(keyBoradHeight-b);
[self.view setFrame:viewCGrect]; }

最后移除通知

-(void)viewWillDisappear:(BOOL)animated
{
[super viewWillDisappear:animated];
[[NSNotificationCenter defaultCenter] removeObserver:self
name:UIKeyboardWillHideNotification
object:nil];
[[NSNotificationCenter defaultCenter] removeObserver:self
name:UIKeyboardDidShowNotification
object:nil]; }

最终测试改方式只适用于输入控件比较少的界面,更多的可以使用gitHub上第三方

DaidoujiChen / DaiDodgeKeyboard

hackiftekhar / IQKeyboardManager


键盘遮挡输入框的问题 - 小小流浪 - 博客园

最新文章

  1. 使用SqlDataReader和SqlDataAdapter的注意
  2. Winform菜单和工具栏控件
  3. jquery实现简单瀑布流布局
  4. 每天一个linux命令(28):tar命令
  5. django1.9 + uwsgi +nginx1.9 部署(centos6.6)
  6. Android 操作SQLite基本用法
  7. D3D11中的MSAA
  8. php防止表单重复提交
  9. Codeforces Round #128 (Div. 2)
  10. 1-4-1 Windows应用程序组成及编程步骤
  11. H264相关随笔
  12. Mysql 常用SQL语句集锦
  13. mysql的sql_mode设置
  14. Dicom文件转mhd,raw文件格式
  15. jQuery实现购物车物品数量的加减
  16. 对象克隆(C# 快速高效率复制对象另一种方式 表达式树转)
  17. Linux中日期的加减运算
  18. L1-055 谁是赢家
  19. spring+mybaits xml配置解析----转
  20. python Count类(转)

热门文章

  1. (转)Python3.5 day3作业二:修改haproxy配置文件
  2. Spark Streaming简介
  3. 文件选择之OpenFileDialog控件
  4. 自写Jq动画载入插件
  5. JavaScript比较和逻辑运算符
  6. Design Pattern -&gt;Abstract Factory
  7. u-boot分析(六)----时钟初始化
  8. Javascript基础--数据类型
  9. 易客CRM-3.0.4 (OpenLogic CentOS 6.5)
  10. Linux CentOS下部署Java Web项目