最正规的办法,用通知
step 1:
在进入视图的时候添加监视:(viewDidLoad什么的)

 

复制代码

  1. // Observe keyboard hide and show notifications to resize the text view appropriately.
  2. [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillShow:) name:UIKeyboardWillShowNotification object:nil];
  3. [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillHide:) name:UIKeyboardWillHideNotification object:nil];

step 2:
在键盘动作的时候移动视图:

 

复制代码

  1. - (void)keyboardWillShow:(NSNotification *)notification {
  2. /*
  3. Reduce the size of the text view so that it's not obscured by the keyboard.
  4. Animate the resize so that it's in sync with the appearance of the keyboard.
  5. */
  6. NSDictionary *userInfo = [notification userInfo];
  7. // Get the origin of the keyboard when it's displayed.
  8. NSValue* aValue = [userInfo objectForKey:UIKeyboardFrameEndUserInfoKey];
  9. // Get the top of the keyboard as the y coordinate of its origin in self's view's coordinate system. The bottom of the text view's frame should align with the top of the keyboard's final position.
  10. CGRect keyboardRect = [aValue CGRectValue];
  11. keyboardRect = [self.view convertRect:keyboardRect fromView:nil];
  12. CGFloat keyboardTop = keyboardRect.origin.y;
  13. CGRect newTextViewFrame = self.view.bounds;
  14. newTextViewFrame.size.height = keyboardTop - self.view.bounds.origin.y;
  15. // Get the duration of the animation.
  16. NSValue *animationDurationValue = [userInfo objectForKey:UIKeyboardAnimationDurationUserInfoKey];
  17. NSTimeInterval animationDuration;
  18. [animationDurationValue getValue:&animationDuration];
  19. // Animate the resize of the text view's frame in sync with the keyboard's appearance.
  20. [UIView beginAnimations:nil context:NULL];
  21. [UIView setAnimationDuration:animationDuration];
  22. textView.frame = newTextViewFrame;
  23. [UIView commitAnimations];
  24. }
  25. - (void)keyboardWillHide:(NSNotification *)notification {
  26. NSDictionary* userInfo = [notification userInfo];
  27. /*
  28. Restore the size of the text view (fill self's view).
  29. Animate the resize so that it's in sync with the disappearance of the keyboard.
  30. */
  31. NSValue *animationDurationValue = [userInfo objectForKey:UIKeyboardAnimationDurationUserInfoKey];
  32. NSTimeInterval animationDuration;
  33. [animationDurationValue getValue:&animationDuration];
  34. [UIView beginAnimations:nil context:NULL];
  35. [UIView setAnimationDuration:animationDuration];
  36. textView.frame = self.view.bounds;
  37. [UIView commitAnimations];
  38. }

step 3:
在退出视图的时候注销通知
viewDidUnload:

 

复制代码

  1. [[NSNotificationCenter defaultCenter] removeObserver:self name:UIKeyboardWillShowNotification object:nil];
  2. [[NSNotificationCenter defaultCenter] removeObserver:self name:UIKeyboardWillHideNotification object:nil];

dealloc:

 

复制代码

  1. [[NSNotificationCenter defaultCenter] removeObserver:self name:nil object:nil];

这些代码是摘自apple sample code KeyboardAccessory.
些许细节自己修改下就好了,比如那个textView

ios如何实现被键盘遮挡时,带有textfield的tableview自动上移的更多相关文章

  1. iOS Android中 h5键盘遮挡输入框的问题和解决方案

    问题发现:在 Android 部分机型 和 iOS部分系统下 键盘会出现遮挡输入框的情况(壳内).问题解决: Android 经过测试,Android 的6.0版本以上均会出现改问题,归根到底是之前的 ...

  2. iOS tableview上textView在编辑状态时,tableview自动上移的功能

    在viewcognroller中,添加tableview时, tableview中cell上的textField如果吊起键盘时,tableview时可以自动上移,但是如果是textView吊起键盘,t ...

  3. 键盘遮挡控件(textfield/textview.......)

    采用的是通知的常规方式 // 解决键盘遮挡问题//选择didShow是因为需要键盘的高度//选择willHide是因为视图frame重置需要优先于键盘消失,否则表现得不连贯 [[NSNotificat ...

  4. IOS系统下虚拟键盘遮挡文本框问题的解决

    最近在项目中发现同样的代码在Android端微信网页中点击文本框唤出的虚拟键盘不会遮挡文本框,但是在IOS端的微信网页中点击文本框唤出的键盘却在大部分情况下会遮挡文本框 经过高人指点,这个问题终于解决 ...

  5. android 记一次解决键盘遮挡问题

    文章链接:https://mp.weixin.qq.com/s/1gkMtLu0BTXOUOj6isDjUw 日常android开发过程中,会遇到编辑框输入内容弹出软键盘,往往会出现键盘遮挡内容,或者 ...

  6. iOS解决表格中TextField,TextView编辑时,输入框被键盘遮挡的问题

    方法1:在原来的 UIViewController 内部再添加一层 UITableViewController 代码如下 : // // ViewController.m // 键盘遮挡问题 // / ...

  7. iOS开发笔记11:表单键盘遮挡、浮点数价格格式化显示、省市区选择器、View Debugging

    1.表单键盘遮挡 应用场景为一个collectionView上有多个textfield.textView供用户填写信息. 之前输入项较少时,采取的方法比较粗暴,didSelectItemAtIndex ...

  8. 『零行代码』解决键盘遮挡问题(iOS)

    关注仓库,及时获得更新:iOS-Source-Code-Analyze https://github.com/draveness/iOS-Source-Code-Analyze Follow: Dra ...

  9. iOS键盘遮挡输入框,输入区域自动上移

    在iOS开发过程当中,遇到关于键盘遮挡输入框的问题,经过网络参考与实践,总结如下: 登录窗口,上下放置两个UITextField,一个用户名,一个密码,放置的在屏幕下方1/3处,当点击用户名时,自动弹 ...

随机推荐

  1. Nginx的主要配置参数说明

    #定义Nginx运行的用户和用户组user www www; #nginx进程数,建议设置为等于CPU总核心数.worker_processes 8; #全局错误日志定义类型,[ debug | in ...

  2. 手机端的META

    一.天猫 <title>天猫触屏版</title> <meta content="text/html; charset=utf-8" http-equ ...

  3. bootstrap-datetimepicker年视图中endDate设置之后比正常时间提前两个月

    问题 bootstrap-datetimepicker年视图中endDate设置结束时间为2016-08,(即8月之后的日期不能选)而在日历上显示时为2016-06,相差两个月,即6月之后的日期不能选 ...

  4. SpringMvc基础知识(二) springmvc和mybatis整合

    1 springmvc和mybatis整合 1.1 需求 使用springmvc和mybatis完成商品列表查询. 1.2 整合思路 springmvc+mybaits的系统架构: 第一步:整合dao ...

  5. android 使用开源库zxing生成二维码,扫描二维码【转】

    转自:http://blog.csdn.net/qq_16064871/article/details/52422723 zxing是一个开放源码的,用Java实现的多种格式的1D/2D条码图像处理库 ...

  6. Multi-Paxos协议日志同步应用

    使用Multi-Paxos协议的日志同步与恢复 基于Basic-Paxos协议的日志同步方案, 所有成员的身份都是平等的, 任何成员都可以提出日志持久化的提案, 并且尝试在成员组中进行持久化. 而在实 ...

  7. HTML5 拖拽事件

    dragstart:拖拽开始 drag: 拖拽中,会不停的触发 dragend:拖拽结束 ondraglevea:有元素离开了本元素 ondragenter:有元素进入了本元素 ondragover: ...

  8. hdu 5178(二分-lower_bound,upper_bound)

    pairs Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submi ...

  9. 【转载】一键安装maven脚本

    #!/bin/bashif [ -z "${MAVEN_HOME}" ]; then #得到时间 TIME_FLAG=`date +%Y%m%d_%H%M%S` #备份配置文件 c ...

  10. HDU 2612 Find a way【多起点多终点BFS/两次BFS】

    Find a way Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total Su ...