解决Keyboard遮盖输入的几种办法
一般来说,键盘遮挡主要有这么几种情况,一个是遮住UITextView,还有就是遮住UITextField,一般来说,比较推荐在UIScrollView或者UITableView里加入textfield的控件。但是有时也许难免。。
在UITextView中
- (void)viewWillAppear:(BOOL)animated
{
[super viewWillAppear:animated]; [[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 *)aNotification
{
NSDictionary *userInfo = [aNotification userInfo]; CGRect keyboardRect = [[userInfo objectForKey:UIKeyboardFrameEndUserInfoKey] CGRectValue];
NSTimeInterval animationDuration = [[userInfo objectForKey:UIKeyboardAnimationDurationUserInfoKey] doubleValue]; CGRect newFrame = self.view.frame;
newFrame.size.height -= keyboardRect.size.height; [UIView beginAnimations:@"ResizeTextView" context:nil];
[UIView setAnimationDuration:animationDuration]; self.view.frame = newFrame; [UIView commitAnimations];
}
获取键盘显示的信息,然后根据信息对view的frame进行调整,然后WillHide方法就跟上面相同,只不过把高度新高度改成+= keyboardRect.size.height就可以了,最后,移除观察者:
- (void)viewDidDisappear:(BOOL)animated
{
[super viewDidDisappear:animated]; [[NSNotificationCenter defaultCenter] removeObserver:self
name:UIKeyboardWillHideNotification
object:nil];
[[NSNotificationCenter defaultCenter] removeObserver:self
name:UIKeyboardWillShowNotification
object:nil];
}
在UIView中遮挡UITextField
- (void)keyboardWillShow:(NSNotification *)aNotification
{
NSDictionary *userInfo = [aNotification userInfo]; CGRect keyboardRect = [[userInfo objectForKey:UIKeyboardFrameEndUserInfoKey] CGRectValue];
NSTimeInterval animationDuration = [[userInfo objectForKey:UIKeyboardAnimationDurationUserInfoKey] doubleValue]; CGRect oldFrame = _viewRect; [UIView beginAnimations:@"ResizeView" context:nil];
[UIView setAnimationDuration:animationDuration]; self.view.frame = CGRectMake(oldFrame.origin.x, oldFrame.origin.y - keyboardRect.size.height + 20, oldFrame.size.width, oldFrame.size.height); [UIView commitAnimations];
}
- (void)keyboardWillHide:(NSNotification *)aNotification
{
NSDictionary *userInfo = [aNotification userInfo];
NSTimeInterval animationDuration = [[userInfo objectForKey:UIKeyboardAnimationDurationUserInfoKey] doubleValue]; CGRect newFrame = _viewRect;
newFrame.origin.y += 20; [UIView beginAnimations:@"ResizeView" context:nil];
[UIView setAnimationDuration:animationDuration]; self.view.frame = newFrame; [UIView commitAnimations];
}
UITableView
解决Keyboard遮盖输入的几种办法的更多相关文章
- cocos2d-x解决中文乱码问题的几种办法
昨天改写cocos2d-x的例程,想在其基础上加上一个计分系统.没有分数实在让人没有玩下去的动力! 我在主场景上加上了一个CCLabelTTF,用于显示分数. 但是意外的发现,当内容含有中文时,CCL ...
- Cocos2d-x 3.1.1 学习日志4--cocos2d-x解决中文乱码问题的几种办法
做个打飞机的游戏,由于版本号太新,网上基本没有教教程,我的版本号是cocos2d-x 3.1.1的.今天遇到cocos2dx中中文乱码的问题.无奈仅仅好Google百度寻求答案,明确了这个问题的缘由. ...
- hadoop job解决大数据量关联时数据倾斜的一种办法
转自:http://www.cnblogs.com/xuxm2007/archive/2011/09/01/2161929.html http://www.geminikwok.com/2011/04 ...
- [RN] React Native 键盘管理 在Android TextInput遮盖,上移等问题解决办法
React Native 键盘管理 在Android TextInput遮盖,上移等问题解决办法 解决办法: 打开android工程,在AndroidManifest.xml中配置如下: <ac ...
- eclipse new server Cannot create a server using the selected type 网上有两种办法,其实原理一样
eclipse new server Cannot create a server using the selected type 网上有两种办法,其实原理一样 第一种说法: 还真的找到解决的方法了, ...
- js检测数据类型四种办法
面试题中经常会考js数据类型检测,今天我来分享一下js中常用的四种方法判断数据类型,欢迎指点更正. 废话不多说,直入正题. 1.typeof console.log(typeof "&quo ...
- oracle导出序列的几种办法
oracle导出序列的几种办法 注:本文来源于<oracle导出序列的几种办法> 方法一: select 'create sequence ' ||sequence_name|| ' mi ...
- android 开发 使用自定义布局实现标题栏复用(标题栏内容自定义:使用代码实现和xml布局自定义属性2种办法实现)
在个人学习的情况下可能很少使用自定义布局去实现大量复用的情况下,但是在一个开发工作的环境下就会使用到大量复用的自定义控件. 实现思维: 1.写一个xml的布局,用于标题栏的样式,并且添加在标题栏中你想 ...
- Atitit 解决Unhandled event loop exception错误的办法
Atitit 解决Unhandled event loop exception错误的办法 查看workspace/.metadata/.log org.eclipse.swt.SWTError: No ...
随机推荐
- jvm在什么情况下会执行GC
jvm在什么情况下会执行GC?[五种情况] 对象没有引用 作用域发生未捕获异常 程序在作用域正常执行完毕 程序执行了System.exit() 程序发生意外终止(被杀进程等) 什么是没有对象引用?
- liunx的命令大全
- BZOJ4205 : 卡牌配对
对于两张卡牌,如果存在两种属性值不互质,则可以匹配. 只考虑200以内的质数,一共有46个,可以新建3*46*46个点来表示一类属性值中有这两种质数的卡牌. 然后对于每张卡牌,枚举它的质因子,最多只有 ...
- 【μ'sic forever♪♪♪】μ's Final Love Live周年纪念
一.正文 “切なくて时をまきもどしてみるかい?No no no……いまが最高!” 转眼就是一周年了,其实fl后入坑的我在这里怀念显得有些无病呻吟.但我也有想说的话,说给重要的人听. “ほのかな予感から ...
- nodejs备忘总结(一) -- 基础入门
什么是NodeJS JS是脚本语言,脚本语言都需要一个解析器才能运行.对于写在HTML页面里的JS,浏览器充当了解析器的角色.而对于需要独立运行的JS,NodeJS就是一个解析器. 每一种解析器都是一 ...
- HDU 5842 Lweb and String 水题
Lweb and String 题目连接: http://acm.hdu.edu.cn/showproblem.php?pid=5842 Description Lweb has a string S ...
- Regex.Split
private static List<int> GetThemeIds(string themeList) { const string split = "!===!" ...
- java学习:JMM(java memory model)、volatile、synchronized、AtomicXXX理解
一.JMM(java memory model)内存模型 从网上淘来二张图: 上面这张图说的是,在多核CPU的系统中,每个核CPU自带高速缓存,然后计算机主板上也有一块内存-称为主内(即:内存条).工 ...
- word标题编号与上一级不一致的解决方法
前段时间时间就遇到了这个问题,情形如下: 1.标题内容一 1.1二级标题 1.2二级标题 2.第二个一级标题 1.3第二个一级下的二级标题 1.4.就这样乱了 当时搜索一通后很容易就解决了……于是忘记 ...
- 使用chrome控制台作为日志查看器
很多时候,我们需要查看日志的实时输出,这个时候往往只能选择控制台.我这里的日志框架是serilog,它有一个好处是控制台输出时可以高亮的形式显示其参数结构信息,如下图所示 但也存在许多不方便的地方: ...