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. iOS 修改默认 UserAgent

    User-Agent(用户代理)字符串是Web浏览器用于声明自身型号版本并随HTTP请求发送给Web服务器的字符串,在Web服务器上可以获取到该字符串. UIWebView修改UserAgent UI ...

  2. Go mod graphql-go 的 Replace

    现在在项目中大量的使用 graphql,但用的版本是3年前的版本. 3年前包的url:github.com/neelance/graphql-go 现在的url:github.com/graph-go ...

  3. 如何选字体(font-family)

    一.默认字体情况 1.Window下: 宋体(SimSun):Win下大部分游览器的默认字体,宋体在小字号下(如12px.14px)的显示效果还可以接受,但是字号一大就非常糟糕了,所以使用的时候要注意 ...

  4. DRF框架之ModelSerializer序列化器

    ModelSerializer是Serializer的子类,序列化和反序列化跟Serializer一样. ModelSerializer与常规的Serializer相同,但提供了: 基于模型类自动生成 ...

  5. semantic框架 - selection dropdown自定义样式

    semantic和Bootstrap是我最常用的前端框架. 这次需求,是要修改selection dropdown. 直接贴代码,不解释. .ui.form input:not([type]):foc ...

  6. Django框架(一):MVC设计模式、Django简介

    1. MVC设计模式 MVC设计模式:Model-View-Controller简写. 最早由TrygveReenskaug在1978年提出,是施乐帕罗奥多研究中心(Xerox PARC)在20世纪8 ...

  7. Office 2019 for Mac(office办公套件) v16.34中文版

    Microsoft Office 2019 Mac中文版下载​是微软在Mac发行的Office办公软件套件.包含了新版本的Word.Excel.PowerPoint以及现有的OneNote和Outlo ...

  8. 阿里云 asp.net core nginx 单机部署

    1. dotnet core 安装 https://www.microsoft.com/net/download#core 安装之前要安装依赖:yum install libunwind libicu ...

  9. 7)get方式提交表单和简单处理

    一个带有html的代码:   hello.php <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" ...

  10. 吴裕雄--天生自然 pythonTensorFlow自然语言处理:文本数据预处理--生成训练文件

    import sys import codecs # 1. 参数设置 MODE = "PTB_TRAIN" # 将MODE设置为"PTB_TRAIN", &qu ...