最正规的办法,用通知
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. TCP(一)

    TCP的特点:三次握手.四次挥手.可靠连接.丢包重传.所有的关键词都围绕着可靠传输. 实现可靠传输的核心机制:seq+ack.通过ack判断是否有丢包,是否需要重传. 三次握手 1)初始状态:clie ...

  2. Maven的默认中央仓库以及修改默认仓库&配置第三方jar包从私服下载

    当构建一个Maven项目时,首先检查pom.xml文件以确定依赖包的下载位置,执行顺序如下: 1.从本地资源库中查找并获得依赖包,如果没有,执行第2步. 2.从Maven默认中央仓库中查找并获得依赖包 ...

  3. [NOIP 2010] 引水入城

    搜索+贪心. 参考博客:http://blog.sina.com.cn/s/blog_8442ec3b0100xib1.html 主要是要看出来,如果有解的话,每个沿湖城市能够流到的范围是连续的区间. ...

  4. python--celery

    有些时候我们的一些任务比较耗时,比如我们写了一个网站,用户注册的时候需要发送邮件.但是发送邮件的过程比较耗时,用户必须要等到我们将邮件发送成功之后才会得到响应.那么有没有一种办法,当用户点击发送邮件的 ...

  5. JSP 基础之 JSTL <c:if>用法

    <c:if>还有另外两个属性var和scope.当我们执行<c:if>的时候,可以将这次判断后的结果存放到属性var里:scope则是设定var的属性范围.哪些情况才会用到va ...

  6. java中Math.abs(-2147483648)的返回值应该是什么?

    我觉得这是一个非常有意思的问题,Math.abs(-2147483648)的返回值应该是什么? java计算结果 为什么没有得到正数结果呢? 首先我们先看下java区分整数正负的原理.在二进制的情况下 ...

  7. 怎么将string list 转成有特殊字符分开字符串

    https://stackoverflow.com/questions/4021851/join-string-list-elements-with-a-delimiter-in-one-step Y ...

  8. log4j 输入不同日志文件

    log4j的强大功能无可置疑,但实际应用中免不了遇到某个功能需要输出独立的日志文件的情况,怎样才能把所需的内容从原有日志中分离,形成单独的日志文件呢?其实只要在现有的log4j基础上稍加配置即可轻松实 ...

  9. (七)MySQL数据操作DQL:单表查询1

    (1)单表查询 1)环境准备 mysql> CREATE TABLE company.employee5( id int primary key AUTO_INCREMENT not null, ...

  10. Nodejs微信与Bot framework通过Direct Line连接

    背景 最近开发了一个Bot Framework的小工具,能够通过Luis分析出用户输入的文本,对文本中的StyleNo/FabricNo/TrimNo提取出来,并传入另一个小系统进行查询出结果,包括文 ...