采用的是通知的常规方式

    // 解决键盘遮挡问题
//选择didShow是因为需要键盘的高度
//选择willHide是因为视图frame重置需要优先于键盘消失,否则表现得不连贯
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(keyboardWasShown:)
name:UIKeyboardDidShowNotification
object:nil]; [[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(keyboardWasHide:)
name:UIKeyboardWillHideNotification
object:nil];

触发的方法

-(void)keyboardWasShown:(NSNotification*)notification
{
//获取键盘高度
NSValue* value=notification.userInfo[UIKeyboardFrameBeginUserInfoKey];
CGFloat keyBoradHeight=[value CGRectValue].size.height;
NSNumber* animationTime=notification.userInfo[UIKeyboardAnimationDurationUserInfoKey];
double time=[animationTime doubleValue];
//获取被遮挡控件距离controller底部的距离
float a=self.view4.frame.origin.y+self.view4.frame.size.height;
float b = self.view.frame.size.height-a;
if (b<keyBoradHeight)
{
//动画效果
[UIView animateWithDuration:time
animations:^
{
//键盘被挡住了
CGRect viewCGrect=self.view.frame;
//视图应该上移所以是-
viewCGrect.origin.y=viewCGrect.origin.y-(keyBoradHeight-b);
[self.view setFrame:viewCGrect]; } completion:nil]; } }
-(void)keyboardWasHide:(NSNotification*)notification
{
NSValue* value=notification.userInfo[UIKeyboardFrameBeginUserInfoKey];
CGFloat keyBoradHeight=[value CGRectValue].size.height; float a=self.view4.frame.origin.y+self.view4.frame.size.height;
float b = self.view.frame.size.height-a; CGRect viewCGrect=self.view.frame;
viewCGrect.origin.y=viewCGrect.origin.y+(keyBoradHeight-b);
[self.view setFrame:viewCGrect]; }

最后移除通知

-(void)viewWillDisappear:(BOOL)animated
{
[super viewWillDisappear:animated];
[[NSNotificationCenter defaultCenter] removeObserver:self
name:UIKeyboardWillHideNotification
object:nil];
[[NSNotificationCenter defaultCenter] removeObserver:self
name:UIKeyboardDidShowNotification
object:nil]; }

最终测试改方式只适用于输入控件比较少的界面,更多的可以使用gitHub上第三方

DaidoujiChen / DaiDodgeKeyboard

hackiftekhar / IQKeyboardManager


键盘遮挡输入框的问题 - 小小流浪 - 博客园

键盘遮挡控件(textfield/textview.......)的更多相关文章

  1. [Swift]键盘遮挡控件

    键盘遮挡控件: super.viewDidLoad(){ // Do any additional setup after loading the view, typically from a nib ...

  2. android 自定义空间 组合控件中 TextView 不支持drawableLeft属性

    android 自定义空间 组合控件中 TextView 不支持drawableLeft属性.会报错Caused by: android.view.InflateException: Binary X ...

  3. Android零基础入门第17节:Android开发第一个控件,TextView属性和方法大全

    原文:Android零基础入门第17节:Android开发第一个控件,TextView属性和方法大全 前面简单学习了一些Android UI的一些基础知识,那么接下来我们一起来详细学习Android的 ...

  4. Android UI控件:TextView

    TextVIew的属性详解 android:autoLink设置是否当文本为URL链接/email/电话号码/map时,文本显示为可点击的链接.可选值(none/web /email/phone/ma ...

  5. android内部培训视频_第三节 常用控件(Button,TextView,EditText,AutocompleteTextView)

    第三节:常用控件 一.Button 需要掌握的属性: 1.可切换的背景 2.9.png使用 3.按钮点击事件 1)  onClick 3) 匿名类 4) 公共类 二.TextView 常用属性 1.a ...

  6. 跟我学android-常用控件之 TextView

    TextView 是Android文本控件,用于显示文字. 我们先看一看TextView的结构(developer.android.com) 从这里我们可以得知,TextView是View的子类,他有 ...

  7. android基本控件学习-----TextView

    一.TextView的讲解 <实例一> <?xml version="1.0" encoding="utf-8"?> <Linea ...

  8. EXTJS 4.2 资料 控件textfield中fieldLabel去掉冒号,控件label的长度

    代码: labelSeparator: '', // 去掉laebl中的冒号 labelWidth: 10,//控件label的长度

  9. winform自定义控件中其他遮挡控件点击事件

    自定义控件在其他窗口调用时,里面的lable阻挡了控件的点击事件 解决方法 自定义控件中lable的 点击事件 private void Lable1_Click(object sender, Eve ...

随机推荐

  1. (转)AIX 用户和组管理

    AIX 用户和组管理 原文:https://www.ibm.com/developerworks/cn/aix/library/au-aixuseradmin/ 管理 IBM AIX 中的用户和组是管 ...

  2. ThreadPoolExecutor的三种队列SynchronousQueue,LinkedBlockingQueue,ArrayBlockingQueue

    SynchronousQueue SynchronousQueue是无界的,是一种无缓冲的等待队列,但是由于该Queue本身的特性,在某次添加元素后必须等待其他线程取走后才能继续添加:可以认为Sync ...

  3. httpd编译安装php

    wget http://hk1.php.net/distributions/php-5.6.31.tar.gz yum groupinstall "Development Tools&quo ...

  4. HDU 4357——String change——————【规律题】

    String change Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Tot ...

  5. C#表格文字多收缩样式

    C#表格文字多收缩样式 <style> .divOpen { height: 24px; overflow: hidden; } </style> -------------- ...

  6. dapper.net框架使用随笔

    一.简单介绍 Dapper是轻量级的ORM工具,代码就SqlMapper.cs一个文件,对于习惯使用原生的sql语句用户是个好选择,具有以下特性. 1.类似 ado.net 的写法,灵活拼接sql 2 ...

  7. Oracle Functions转成Ms-Sql procedure

    最近公司的一些Oracle项目要转到Ms_sql上,在把Oracle Functions改成MS-Sql的Procedure时,遇到了翻译的问题. 罗列出这些问题: 一.Oracle 基本类型 ora ...

  8. iOS UIWebView 和 WKWebView 的 cookie 获取,设置,删除

    Cookie简介说到Cookie,或许有些小伙伴会比较陌生,有些小伙伴会比较熟悉.如果项目中,所有页面都是纯原生来实现的话,一般Cookie这个东西或许我们永远也不会接触到.但是,这里还是要说一下Co ...

  9. AngularJS 指令解析(一)

    AngularJS 指令解析(一) 前言 笔者AngularJS接触时间差不多是两年多,虽然这两年多AngularJS版本日新月异,但是笔者的版本是比较老的1.4.3,一方面是自己对这个版本比较熟悉, ...

  10. canvas制作雪花效果

    <!DOCTYPE html><html> <head>    <meta http-equiv="Content-type" conte ...