1.当view是非可以滚动的view时,

// 添加对键盘的通知
- -(void)viewDidLoad{
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillShow:) name:UIKeyboardWillShowNotification object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillHide:) name:UIKeyboardWillHideNotification object:nil];
}
- -(void)keyboardWillShow:(NSNotification *)sender{
{ // 得到键盘的高度
NSDictionary *dict = [sender userInfo];
CGSize keyboardSize = [dict[@"UIKeyboardFrameEndUserInfoKey"] CGRectValue].size; // 得到输入框CGRectGetMaxY(<#CGRect rect#>)+40到底部的高度
CGFloat height = (SCREEN_H - (_IPText.frame.origin.y + _IPText.frame.size.height + 40));
CGFloat newY = -keyboardSize.height + height; // 如果键盘的高度大于上面的高度
if (keyboardSize.height > height) { [UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration:0.2];
self.view.frame = CGRectMake(0, newY, SCREEN_W, SCREEN_H);
[UIView commitAnimations]; }
} - (void)keyboardWillHide:(NSNotification *)sender
{
[UIView beginAnimations:nil context:NULL];
// 动画时间3s
[UIView setAnimationDuration:0.2]; self.view.frame = CGRectMake(0, 0, SCREEN_W, SCREEN_H); [UIView commitAnimations];
} - (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
[_IPText resignFirstResponder];
}

2.类似于聊天框被遮挡。

- (void)viewDidLoad
{
// 监听键盘的弹出与隐藏
// 利用消息中心,首先监听UIKeyboardWillChangeFrameNotification 键盘frame改变的消息
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardChangeFrame:) name:UIKeyboardWillChangeFrameNotification object:nil];
}
//监听键盘弹出与退出的方法
-(void)keyboardChangeFrame:(NSNotification *)sender
{
/*
UIKeyboardAnimationCurveUserInfoKey = 7;
UIKeyboardAnimationDurationUserInfoKey = "0.25";
UIKeyboardBoundsUserInfoKey = "NSRect: {{0, 0}, {375, 216}}";
UIKeyboardCenterBeginUserInfoKey = "NSPoint: {187.5, 775}";
UIKeyboardCenterEndUserInfoKey = "NSPoint: {187.5, 559}"; //弹出键盘
UIKeyboardFrameBeginUserInfoKey = "NSRect: {{0, 667}, {375, 216}}";
UIKeyboardFrameEndUserInfoKey = "NSRect: {{0, 451}, {375, 216}}"; //隐藏键盘
UIKeyboardFrameBeginUserInfoKey = "NSRect: {{0, 451}, {375, 216}}";
UIKeyboardFrameEndUserInfoKey = "NSRect: {{0, 667}, {375, 216}}";
*/ //transform是相对位移 // 设置窗口的颜色
self.view.window.backgroundColor = self.tableView.backgroundColor; // 0.取出键盘动画的时间
CGFloat duration = [sender.userInfo[UIKeyboardAnimationDurationUserInfoKey] doubleValue]; // 1.取得键盘最后的frame
CGRect keyboardFrame = [sender.userInfo[UIKeyboardFrameEndUserInfoKey] CGRectValue]; // 2.计算控制器的view需要平移的距离
CGFloat transformY = keyboardFrame.origin.y - self.view.frame.size.height; // 3.让UITableView的最后一个cell滚到键盘最上面
// self.view.transform = CGAffineTransformMakeTranslation(0, transformY);
NSIndexPath *lastIndex = [NSIndexPath indexPathForRow:self.messagesFrame.count - 1 inSection:0];
[self.tableView scrollToRowAtIndexPath:lastIndex atScrollPosition:UITableViewScrollPositionBottom animated:YES]; // 4.执行动画(self.view.transform是tableView加上textFiled一起向上移动,也就是整个view)
[UIView animateWithDuration:duration animations:^{
self.view.transform = CGAffineTransformMakeTranslation(0, transformY);
}]; // self.view.transform = CGAffineTransformMakeTranslation(0, -216); }

  

ios 中键盘被遮挡解决方案的更多相关文章

  1. iOS中常见 Crash 及解决方案

    来源:枫影JustinYan 链接:http://justinyan.me/post/1609 一.访问了一个已经被释放的对象 在不使用 ARC 的时候,内存要自己管理,这时重复或过早释放都有可能导致 ...

  2. IOS中键盘隐藏几种方式

    在ios开发中,经常需要输入信息.输入信息有两种方式: UITextField和UITextView.信息输入完成后,需要隐藏键盘,下面为大家介绍几种隐藏键盘的方式. <一> 点击键盘上的 ...

  3. iOS swift 富文本显示 富文本在iOS中使用场景和解决方案

    项目中很多地方都会用到富文本的内容:比如一般的商品详情,视频详情,资讯详情等,运营人员通过后台的富文本编辑器编辑的内容,前端拿到的就是一段富文本的字符串,这富文本大多都是图片和文字的组合.我们今天介绍 ...

  4. iOS中键盘的收起

    在UIViewController中收起键盘,除了调用相应控件的resignFirstResponder方法之外,还有另外三种方法: 重载UIViewController中的touchesBegin方 ...

  5. ios中键盘处理源码

      1:先分别设置各个文本框的键盘类型(inputview)-->在特定键盘中textediting中禁用输入. 2:然后递归绑定各个键盘的工具条(inputaccessview).并且个各个控 ...

  6. H5页面IOS中键盘弹出导致点击错位的问题

    IOS在点击输入框弹出键盘  键盘回缩 后 定位没有相应改变  还有  textarea 也会弹出键盘 $("input").blur(function() { console.l ...

  7. ios中键盘处理适合ipad 和iphone

    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWasShown:) name:UI ...

  8. ios中键盘处理(二)

    设置UIscrollview的背景代码 - (UIImage *) ImageWithColor: (UIColor *) color frame:(CGRect)aFrame { UIGraphic ...

  9. iOS学习——键盘弹出遮挡输入框问题解决方案

    在iOS或Android等移动端开发过程中,经常遇到很多需要我们输入信息的情况,例如登录时要输入账号密码.查询时要输入查询信息.注册或申请时需要填写一些信息等都是通过我们键盘来进行输入的,在iOS开发 ...

随机推荐

  1. 康冕峰IT技术总结博客CSDN索引

    计算1-x内的质数, 结果保存在mysql中. Java 程序员面试笔试宝典 4.1基础知识https://blog.csdn.net/qq_40993412/article/details/1040 ...

  2. Linux--shell 脚本免密码输入

    参考:https://www.cnblogs.com/lixigang/articles/4849527.html #!/bin/bash ssb=" password=mysql data ...

  3. 关于WIN7系统,在运行pycharm时,老出现问题

    今天在pycharm中写python代码的时候,一直跳出一个窗口: 后来经过上网查询,得出针对此类问题的解决办法如下: (1)在运行中输入“Regedit” (2)HKEY_CURRENT_USER— ...

  4. 基础练习--huffman

    问题描述 Huffman树在编码中有着广泛的应用.在这里,我们只关心Huffman树的构造过程. 给出一列数{pi}={p0, p1, …, pn-},用这列数构造Huffman树的过程如下: . 找 ...

  5. 深入分析Java反射(五)-类实例化和类加载

    前提 其实在前面写过的<深入分析Java反射(一)-核心类库和方法>已经介绍过通过类名或者java.lang.Class实例去实例化一个对象,在<浅析Java中的资源加载>中也 ...

  6. HashMap源码分析(一)

    基于JDK1.7 HashMap源码分析 概述 HashMap是存放键值对的集合,数据结构如下: table被称为桶,大小(capacity)始终为2的幂,当发生扩容时,map容量扩大为两倍 Hash ...

  7. cmd执行jmeter命令生成报告的问题。

    现有几个jmeter脚本,准备以命令行的方式执行jmeter脚本,并生成报告. 一.使用python语言处理 1.目录结构 2.说明 jmx目录下是jmeter脚本 result目录下是生成的报告及文 ...

  8. liunx 常用操作(自用)

    Centos7解压文件 tar -zxvf 文件名[test.tar.gz] Centos7安装vim yum -y install vim* Centos7安装ifconfig yum instal ...

  9. PAT Advanced 1138 Postorder Traversal (25) [树的遍历,前序中序转后序]

    题目 Suppose that all the keys in a binary tree are distinct positive integers. Given the preorder and ...

  10. UML-如何创建领域模型?

    以当前迭代中所要设计的需求为界: 1.寻找概念类 2.将其绘制为类图 3.添加关联和属性