在 iOS5 上请求显示键盘时,系统从屏幕底部将键盘滑入上来,位于应用的内容之上。

(墙内:http://mikixiyou.iteye.com/blog/1488302)

如果屏幕中的内容项目比较多,它就可能覆盖住文本输入框之类的对象。你必须调整你的内容,使得输入框保持可见。

你会想到哪些处理方法呢?

第一种,

临时调整窗口中各个视图的大小,使得键盘从下向上占领的区域空白。键盘的高度( keyboard.size.height )是一定的,将视图中所有内容所在区域的 y 值减小到 y-keyboard.size.height 。

该方法有个局限,如果所有内容之和大于窗口减去键盘高度的话,该方法将不能用。

第二种,

将窗口中所有视图嵌入进一个滚动视图对象( UIScrollView )中。在键盘出现时,你将输入框滚动到合适的位置,调整一下滚动视图的内容区域。

这些操作通过一个通知 UIKeyboardDidShowNotification去实现的,逻辑过程如下:

1 、根据通知的字典信息 userInfo 得到键盘的 size 。

2 、根据键盘的 size 中的 height 值,调整滚动视图内容底部的 inset 。

3 、滚动目标视图即文件输入框进入视图中。

简要的代码如下:

1 、实现两个委托方法,用于指定输入框对象。

- (void)textFieldDidBeginEditing:(UITextField *)textField

{

activeField = textField;

}

 

- (void)textFieldDidEndEditing:(UITextField *)textField

{

activeField = nil;

}

2 、注册通知的观察者

- (void)registerForKeyboardNotifications

{

[[NSNotificationCenter defaultCenter] addObserver:self

selector:@selector(keyboardWasShown:)

name:UIKeyboardDidShowNotification object:nil];

 

[[NSNotificationCenter defaultCenter] addObserver:self

selector:@selector(keyboardWillBeHidden:)

name:UIKeyboardWillHideNotification object:nil];

 

}

将这个方法放在 viewDidAppear 中调用。

同时也要写一个 removeObserver 放在 viewWillDisappear 中调用。

3 、实现键盘显示通知的 selector 中的方法

// Called when the UIKeyboardDidShowNotification is sent.

- (void)keyboardWasShown:(NSNotification*)aNotification

{

NSDictionary* info = [aNotification userInfo];

CGSize kbSize = [[info objectForKey:UIKeyboardFrameBeginUserInfoKey] CGRectValue].size;

 

UIEdgeInsets contentInsets = UIEdgeInsetsMake(0.0, 0.0, kbSize.height, 0.0);

scrollView.contentInset = contentInsets;

scrollView.scrollIndicatorInsets = contentInsets;

 

// If active text field is hidden by keyboard, scroll it so it's visible

// Your application might not need or want this behavior.

CGRect aRect = self.view.frame;

aRect.size.height -= kbSize.height;

if (!CGRectContainsPoint(aRect, activeField.frame.origin) ) {

CGPoint scrollPoint = CGPointMake(0.0, activeField.frame.origin.y-kbSize.height);

[scrollView setContentOffset:scrollPoint animated:YES];

}

}

4 、实现键盘消失通知的方法

// Called when the UIKeyboardWillHideNotification is sent

- (void)keyboardWillBeHidden:(NSNotification*)aNotification

{

UIEdgeInsets contentInsets = UIEdgeInsetsZero;

scrollView.contentInset = contentInsets;

scrollView.scrollIndicatorInsets = contentInsets;

}

这个方法调整内容底部的 inset 的值使得输入框不被键盘区域屏蔽的。还可以换种方法实现。

第三种,

扩展内容视图的高度,滚动文本输入框对象进内容视图。

将 keyboardWasShown: 重写。

- (void)keyboardWasShown:(NSNotification*)aNotification {

NSDictionary* info = [aNotification userInfo];

CGSize kbSize = [[info objectForKey:UIKeyboardFrameBeginUserInfoKey] CGRectValue].size;

CGRect bkgndRect = activeField.superview.frame;

bkgndRect.size.height += kbSize.height;

[activeField.superview setFrame:bkgndRect];

[scrollView setContentOffset:CGPointMake(0.0, activeField.frame.origin.y-kbSize.height) animated:YES];

}

如何保持iOS上键盘出现时输入框不被覆盖的更多相关文章

  1. 保持iOS上键盘出现时输入框不被覆盖

    如果屏幕中的内容项目比较多,键盘就可能覆盖住文本输入框之类的对象.你必须调整你的内容,使得输入框保持可见. 你会想到哪些处理方法呢? 第一种, 临时调整窗口中各个视图的大小,使得键盘从下向上占领的区域 ...

  2. iOS之按钮出现时加一个动画效果

    //按钮出现时的动画效果 + (void)buttonAnimation:(UIButton *)sender { CAKeyframeAnimation *animation = [CAKeyfra ...

  3. 手机端上点击input框软键盘出现时把input框不被覆盖,显示在屏幕中间(转)

    转载地址:https://www.cnblogs.com/xzzzys/p/7526761.html 1  用定位为题来解决var oHeight = $(document).height(); // ...

  4. iOS键盘出现时界面跟着往上推

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

  5. ios-利用键盘通知处理键盘出现时遮挡控件问题

    -(void)viewDidLoad { NSNotificationCenter *center = [NSNotificationCenter defaultCenter]; //注册键盘显示通知 ...

  6. ios 避免navigationcontroller出现时scrollview内容被resize

    viewDidLoad中设置以下属性 self.automaticallyAdjustsScrollViewInsets = NO;

  7. iOS 监听键盘高度,输入框上升

    //设置输入框 ---<因为输入框用了get方法,所以第一次调用输入框要用self 调用>: self.textlab.frame=CGRectMake(, , , ); _textlab ...

  8. iOS UIWebView键盘处理

    让UIWebView弹出键盘上的按钮显示中文    http://www.cr173.com/html/19440_1.html 如果你有下面的问题,此文也许会帮到你. 键盘遮盖了UIWebView. ...

  9. iOS UIWebView键盘操控

    +-------------------------+ 假设你有以下的问题,也许这篇文章将帮助你. 键盘遮盖了UIWebView. 怎样拖动UIWebView来移除键盘. 键盘出现时UIWebView ...

随机推荐

  1. VMware 命令行下的网络配置

    一般我们会在windows操作系统上安装VMware虚拟机,然后在虚拟机中安装Linux或Unix系统,但是我们很多人习惯了Windows系统的图形界面,换到Linux或Unix系统下不知道怎么配置网 ...

  2. POJ 2031 Building a Space Station (最小生成树)

    Building a Space Station Time Limit: 1000MS   Memory Limit: 30000K Total Submissions: 5173   Accepte ...

  3. 剑指Offer22 判断数组是否为某二叉搜索树的后序遍历

    /************************************************************************* > File Name: 22_Sequen ...

  4. Google IP 最新地址

    原文地址:https://ideas.spkcn.com/technology/250.html 2015年 目前最新可以直接访问google的IP91.213.30.152173.194.77.14 ...

  5. poj 1821 动态规划

    思路:每次枚举每个工人的右边界j,维护最优的左边界k.那么dp[j]=max(dp[j],dp[k]+(j-k)*w[i].p): 对于每个工人的初值k=w[i].s-1; 令x=j-w[i].l,如 ...

  6. jsonString转NSDictionary

    NSData *webData = [ \": {\"name\": \"Jerry\",\"age\": \"12\& ...

  7. P2342 叠积木

    P2342 叠积木 17通过 66提交 题目提供者wwqk4444 标签树状数组线段树USACO 难度普及+/提高 提交该题 讨论 题解 记录 最新讨论 暂时没有讨论 题目背景 Cube Stacki ...

  8. html&css静态页面

    状态不好,整晚未眠. 想着敲点代码,遇着复杂的又自己生气,所以就敲了博客园的,总是很纠结"哪样的文字算标题算段落或要用span""什么时候用div比较好"&qu ...

  9. 应用越来越广泛的css伪类

    说起css伪类,学习web前端网页设计的同学们应该对此应该不是很陌生,以前很多的网页的特效大多是通过js来实现的.但是随着CSS3不断开发,利用css实现网页的特效不仅响应不错,而且还减少了很多的代码 ...

  10. 别人网站生成的json

    HttpWebRequest req = (HttpWebRequest)WebRequest.Create(sb.ToString()); WebResponse rep = req.GetResp ...