最正规的办法,用通知
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. DotNETCore 学习笔记 配置

    Configuration var builder = new ConfigurationBuilder(); builder.AddInMemoryCollection(); var config ...

  2. Mac git

    多次提交代码,与合并的工作量成反比. in terminal window git status:查看git的状态 git add -A: 把所有不再track里的文件加入进去/ git add -u ...

  3. js事件中绑定另一事件导致事件多次执行

    1.html代码 <input type="button" value="add"> <input type="button&quo ...

  4. echarts图表自适应浏览器窗口的大小

    echarts问题描述 当浏览器窗口发生变化时,echarts图表未能自适应浏览器的变化,产生如下的效果 解决方案 在$(function(){}中添加 window.onresize = funct ...

  5. 手把手教你配置苹果APNS推送服务|钿畑的博客 | 钿畑的博客

    http://www.360doc.com/content/15/0118/17/1073512_441822850.shtml# 钿畑的文章索引 1. 什么是推送通知 2. 什么是APNS? 3. ...

  6. NetBeans中从实体创建Restful webservice工程

    分布式系统和移动计算...... 这学期上的课,名字听起来是不是很霸气? 然而 其实就是 restful 和 安卓...... 汗....... 用的IDE还是netBeans, 第一次听说有这个ID ...

  7. 【转】Ubuntu 14.04.3上配置并成功编译Android 6.0 r1源码

    http://www.linuxidc.com/Linux/2016-01/127292.htm 终于成功把Android 6.0 r1源码的源码编译.先上图,这是在Ubuntu中运行的Android ...

  8. PHP实现innodb的数据回滚

    //实例化的数据库对象$model = new Model();//开启事务$model->startTrans();//默认结果$judge = true;//数据操作$sql1 =" ...

  9. 错误 NETSDK1068: 框架依赖型应用程序主机需要一个至少 “netcoreapp2.1” 的目标框架

    错误 NETSDK1068: 框架依赖型应用程序主机需要一个至少 “netcoreapp2.1” 的目标框架 我有一个ASP.NET Core 2网站应用程序,编译运行都没有问题,但是发布时却出了错, ...

  10. String.Format,DateTime日期时间格式化

    DateTime dt = DateTime.Now;//2010年10月4日 17点05分            string str = "";            //st ...