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

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

第一种,

临时调整窗口中各个视图的大小,使得键盘从下向上占领的区域空白。键盘的高度( 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上键盘出现时输入框不被覆盖

    在 iOS5 上请求显示键盘时,系统从屏幕底部将键盘滑入上来,位于应用的内容之上. (墙内:http://mikixiyou.iteye.com/blog/1488302) 如果屏幕中的内容项目比较多 ...

  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. 原生js显示分页效果

    <!DOCTYPE html> <html> <head lang="en"> <meta charset="UTF-8&quo ...

  2. BC Harry and Magical Computer (拓扑排序)

    Harry and Magical Computer  Accepts: 350  Submissions: 1348  Time Limit: 2000/1000 MS (Java/Others) ...

  3. codeforces 680A A. Bear and Five Cards(水题)

    题目链接: A. Bear and Five Cards //#include <bits/stdc++.h> #include <vector> #include <i ...

  4. 访问iPhone通讯录的问题

    每个APP只显示一次上图的提示,即使卸载APP也不显示.如果想再次显示提示,可进入 设置-->通用-->还原-->还原位置与隐私,即可. 而且在ios6中文环境下,访问通讯录会出现错 ...

  5. miniui MVC datagrid数据绑定

    数据绑定 Default.cshtml <div id="datagrid1" class="mini-datagrid" style="wid ...

  6. Python调用Webservice、访问网页

    昨天在调试Webservice的时候,由于不想写测试程序,就想用Python访问Webservice,结果还是相当的麻烦.远没有VSIDE用的方便 不得不说VS还是很强大的,人性化做的很好,不需要你看 ...

  7. linux中mail函数不能发送邮件

    没有安装或启动 sendmail 组件 解决办法 我是新手,命令不熟,所以写的很详细,老鸟勿喷哦 1.重新安装 sendmail 组件,我用的是 CentOS ,使用下面的命令安装 代码如下 复制代码 ...

  8. State模式

    地铁十字转门 状态迁移表格. 起始状态 触发迁移的事件 终止状态  要执行的动作. Locked   Coin               UnLocked UnLock UnLocked Pass  ...

  9. hdu 1963 Investment 多重背包

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1963 //多重背包 #include <cstdio> #include <cstr ...

  10. hidden(隐藏域)

    <input type="hidden">? 这等于是隐藏值,客户端无需显示,但提交后后台能够接受,例如你放个表单验证值在里边,这样可以拒绝来源不明的表单提交.还有判断 ...