采用的是通知的常规方式

    // 解决键盘遮挡问题
//选择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. 移动端的click点透问题

    在移动端开发中,有时会出现click点透的问题. 一.什么是click点透 以下情况,在B元素上有半透明红色遮盖层A,黄色B元素内有可点击链接C. tips:以下举例仅针对webkit内核浏览器,所有 ...

  2. [转]jQuery TextBox Water Mark with asp.net

    本文转自:http://naspinski.net/post/jQuery-TextBox-Water-Mark-with-aspnet.aspx I stole majority of this c ...

  3. Linux大文件快速处理小方法

    背景 工作中使用MapReduce任务导出一批含有路径的文件,共计行数300W+,需要检测文件是否在对应的服务器中存在,而文件所在的服务器并非hadoop集群的服务器,因此打算采用bash脚本进行.具 ...

  4. springboot 整合redisson

    整合代码已经过测试 1.pom <!-- redisson --> <dependency> <groupId>org.redisson</groupId&g ...

  5. 结合IL和Windbg来看.Net调用继承虚方法的执行顺序

    先上测试代码: using System; using System.Collections.Generic; using System.Linq; using System.Text; using ...

  6. MarkDown 编辑器学习

    MarkDown 编辑器学习 是一种简单快键的文字排版工具,可以用于编写说明文档,鉴于其语法简洁明了,且其渲染生成的样式简单美观,很多开发者也用它来写博客,已被国内外很多流行博客平台所支持.生成的文件 ...

  7. js之静态方法与实例方法

    静态方法是指不需要声明类的实例就可以使用的方法. 实例方法是指必须要先使用"new"关键字声明一个类的实例, 然后才可以通过此实例访问的方法. function staticCla ...

  8. javascript中如何实现继承

    javascript中如何实现继承 // 原型方式的'继承' function Person(name) { //定义一个Person的构造函数 this.name = name; //添加属性 } ...

  9. 海量数据去重之SimHash算法简介和应用

    SimHash是什么 SimHash是Google在2007年发表的论文<Detecting Near-Duplicates for Web Crawling >中提到的一种指纹生成算法或 ...

  10. selenium select 标签选中

    public static int SetSelectedIndex(this IWebDriver webdriver, string selector, int selectedIndex) { ...