ios 中键盘被遮挡解决方案
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 中键盘被遮挡解决方案的更多相关文章
- iOS中常见 Crash 及解决方案
来源:枫影JustinYan 链接:http://justinyan.me/post/1609 一.访问了一个已经被释放的对象 在不使用 ARC 的时候,内存要自己管理,这时重复或过早释放都有可能导致 ...
- IOS中键盘隐藏几种方式
在ios开发中,经常需要输入信息.输入信息有两种方式: UITextField和UITextView.信息输入完成后,需要隐藏键盘,下面为大家介绍几种隐藏键盘的方式. <一> 点击键盘上的 ...
- iOS swift 富文本显示 富文本在iOS中使用场景和解决方案
项目中很多地方都会用到富文本的内容:比如一般的商品详情,视频详情,资讯详情等,运营人员通过后台的富文本编辑器编辑的内容,前端拿到的就是一段富文本的字符串,这富文本大多都是图片和文字的组合.我们今天介绍 ...
- iOS中键盘的收起
在UIViewController中收起键盘,除了调用相应控件的resignFirstResponder方法之外,还有另外三种方法: 重载UIViewController中的touchesBegin方 ...
- ios中键盘处理源码
1:先分别设置各个文本框的键盘类型(inputview)-->在特定键盘中textediting中禁用输入. 2:然后递归绑定各个键盘的工具条(inputaccessview).并且个各个控 ...
- H5页面IOS中键盘弹出导致点击错位的问题
IOS在点击输入框弹出键盘 键盘回缩 后 定位没有相应改变 还有 textarea 也会弹出键盘 $("input").blur(function() { console.l ...
- ios中键盘处理适合ipad 和iphone
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWasShown:) name:UI ...
- ios中键盘处理(二)
设置UIscrollview的背景代码 - (UIImage *) ImageWithColor: (UIColor *) color frame:(CGRect)aFrame { UIGraphic ...
- iOS学习——键盘弹出遮挡输入框问题解决方案
在iOS或Android等移动端开发过程中,经常遇到很多需要我们输入信息的情况,例如登录时要输入账号密码.查询时要输入查询信息.注册或申请时需要填写一些信息等都是通过我们键盘来进行输入的,在iOS开发 ...
随机推荐
- Tomcat启动失败原因: More than one fragment with the name [spring_web] was found. 解决
将一个eclipse上搭建好的项目移到idea开发时遇到的问题,tomcat启动时报了3个错误 -Nov- :: ms -Nov- ::)-127.0.0.1] org.apache.tomcat.u ...
- i春秋-web-爆破-1
题目内容:flag就在某六位变量中. 题目 include "flag.php"; $a = @$_REQUEST['hello']; if(!preg_match('/^\w*$ ...
- D10 基本数据类型(各种职业的技能分析) 主要为 int 和 str
在python中具有魔法的 职业 类型 召唤每种职业 在pychar 中 打出该职业的名称 按住Ctrl 光标在该职业名称上 点击就能看该职业的技能 1 数字 int a = " ...
- POJ 2976 Dropping tests【0/1分数规划模板】
传送门:http://poj.org/problem?id=2976 题意:给出组和,去掉对数据,使得的总和除以的总和最大. 思路:0/1分数规划 设,则(其中等于0或1) 开始假设使得上式成立,将从 ...
- usert
usert类型 不是一个函数,而是一个语言构造器 usert后会不会释放内存 当usert的文件大于2044KB时才会释放内存,否则不释放内存
- python语法基础-并发编程-进程-进程锁和进程间通信
############### 守护进程 ############## """ 守护进程 父进程中将一个子进程设置为守护进程,那么这个子进程会随着主进程的结束而结束 ...
- flask框架-大结局
flask-script 用于实现类似于django中 python3 manage.py runserver ...类似的命令. 安装 pip3 install flask-script 使用: f ...
- win10下安装cygwin全过程
简单讲:cygwin就是在windows系统上跑linux和unix的环境,跨平台移植的应用程序移植. 安装步骤: 下载cygwin: 打开官网https://cygwin.com/install.h ...
- aiohttp web服务端(server)样例 (非client)
python版本 python3.6 (其他版本需要小改,版本>python3.4) 参考网址:https://www.cnblogs.com/ameile/p/5589808.html as ...
- 用Plotily处理数据的基本操作
import pandas as pd # 导入数据.scv df = pd.read_csv(" .csv") # 查看前五行数据 df.head() # 查看一下数据描述 df ...