最正规的办法,用通知
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. [bzoj3238][Ahoi2013]差异——后缀自动机

    Brief Description Algorithm Design 下面给出后缀自动机的一个性质: 两个子串的最长公共后缀,位于这两个串对应的状态在parent树上的lca状态上.并且最长公共后缀的 ...

  2. Linux下安装php环境并且配置Nginx支持php-fpm模块[www]

    Linux下安装php环境并且配置Nginx支持php-fpm模块 http://www.cnblogs.com/freeweb/p/5425554.html 5分钟搭建 nginx +php --- ...

  3. time_t转化成日期格式小工具

    time_t转化成日期格式小工具下载  http://files.cnblogs.com/files/lansan0701/TimeTool.zip

  4. 【转】JSP自定义标签

    转载自:http://www.cnblogs.com/edwardlauxh/archive/2010/05/20/1918587.html tld标签的描述文件 标签的描述文件是一个描述整个标签库标 ...

  5. 关于transform的属性

    transfrom的出现让许多静态的东西动了起来,让网页更加具有生命力. 在transform属性中,transform-origin属性仅是其中之一,要彻底理解transform属性,这是不够的,必 ...

  6. nginx部署vue工程和反向代理nodejs工程

    前端是vue,后端是nodejs 前端打包成dist目录,后端接口是localhost:4000/api server { listen 80; #listen [::]:80; server_nam ...

  7. Spring ClassPathXmlApplicationContext和FileSystemXmlApplicationContext读取配置文件的方法

    先说:ClassPathXmlApplicationContext 这个类,默认获取的是WEB-INF/classes/下的路径,也就是在myeclipse的src下的路径,所以用这个是获取不到WEB ...

  8. Delphi:对TNotifyEvent的理解

    type TNotifyEvent = procedure (Sender: TObject) of object; 在Delphi中事件也是一个类,类型就是事件类型,不同的事件属于不同的类.TNot ...

  9. 自定义的一个JDBC工具类

    package JDBCutils; import java.io.File;import java.io.FileInputStream;import java.sql.Connection;imp ...

  10. React-Native集成dva.js

    dvajs作为一个基于redux的状态管理框架,在react中的表现还是很不错的,如果我们想要在react-native应用中使用dvajs该怎么做呢? 首先安装dva-core和react-redu ...