最正规的办法,用通知
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. 会话Cookie

    Cookie分为会话Cookie和本地Cookie两种 之前一直理解的是会话Cookie不在本地文件存储,只存储于内存,而本地Cookie因为设置了expire过期时间需要在本地存储 下面是白帽子讲W ...

  2. python学习 - yield

    def myYield2(): for i in range(3): yield '2222 i am in myYield2', 'i = ', i def myYield(): for i in ...

  3. postgresql数据库备份和恢复(超快)

    PostgreSQL自带一个客户端pgAdmin,里面有个备份,恢复选项,也能对数据库进行备份 恢复(还原),但最近发现数据库慢慢庞大的时候,经常出错,备份的文件过程中出错的几率那是相当大,手动调节灰 ...

  4. hibernate基础学习

    转载自:http://blog.csdn.net/fb281906011/article/details/17628111 一:下载hibernate:http://hibernate.org/orm ...

  5. 无界面运行Jmeter压测脚本

    今天在针对单一接口压测时出现了从未遇到的问题,设好并发量后用调度器控制脚本的开始和结束,但在脚本应该自动结束时间,脚本却停不下来,手动stop报告就会有error率,卡了我很久很久不能解决,网络上也基 ...

  6. rest-framework-@action()装饰器

    路由Routers 使用方法:   在urls.py中定义路由 from rest_framework.routers import DefaultRouter # 定义视图集的路由 router = ...

  7. Stack的三种含义 ----超级经典 明白了 栈 的三种含义

    来自:http://www.ruanyifeng.com/blog/2013/11/stack.html ----------------------------------------------- ...

  8. Selenium2+python自动化52-unittest执行顺序【转载】

    前言 很多初学者在使用unittest框架时候,不清楚用例的执行顺序到底是怎样的.对测试类里面的类和方法分不清楚,不知道什么时候执行,什么时候不执行. 本篇通过最简单案例详细讲解unittest执行顺 ...

  9. k8s创建资源的两种方式

    命令 vs 配置文件 Kubernetes 支持两种方式创建资源: 1. 用 kubectl 命令直接创建 kubectl run nginx-deployment --image=nginx: -- ...

  10. mybatis-config.xml的解释(zz)

    <!-- xml标准格式 --><?xml version="1.0" encoding="UTF-8"?>  <!DOCTYPE ...