解决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 ...
随机推荐
- centos 7 安装 BeautifulSoup 和requests
安装beautifulsoup wget https://www.crummy.com/software/BeautifulSoup/bs4/download/4.5/beautifulsoup4-4 ...
- 贝壳找房魔法师顾问[并查集+DAG判断]
题目链接[https://nanti.jisuanke.com/t/27647] //计蒜客2018复赛D题,想简单了. 题解: 题目是中文的,不再赘述. 题解: 分为三种情况:1.两个字符串都不能变 ...
- 【BZOJ-3456】城市规划 CDQ分治 + NTT
题目链接 http://www.lydsy.com/JudgeOnline/problem.php?id=3456 Solution 这个问题可以考虑dp,利用补集思想 N个点的简单图总数量为$2^{ ...
- CentOS下多网卡绑定多IP段时导致只有一个会通的问题解决
原因:Linux默认开启了反向路由检查导致的,比如说外面访问eth0的网卡,而网关在eth1上,又或者从eth0出的流量,而网关在eth1上,此时会检查到网关不在同一个网卡上导致出不去,进不来的问题. ...
- setTimeout 的黑魔法 【event loop】
setTimeout,前端工程师必定会打交道的一个函数.它看上去非常的简单,朴实.有着一个很不平凡的名字--定时器.让年少的我天真的以为自己可以操纵未来.却不知朴实之中隐含着惊天大密.我还记得我第一次 ...
- 委托、Lambda表达式、事件系列01,委托是什么,委托的基本用法,委托的Method和Target属性
委托是一个类. namespace ConsoleApplication1 { internal delegate void MyDelegate(int val); class Program { ...
- .net项目中使用Quartz
(1)在web.config中进行相关配置 <configSections> <section name="quartz" type="System.C ...
- C#高级编程小结
小结 这几章主要介绍了如何使用新的dynamic类型,还讨论了编译器在遇到dynamic类型时会做什么.还讨论了DLP,可以把它包含在简单的应用程序中.并通过Pythin使用DLR,执行Python脚 ...
- C#编程(六十五)----------表达式树
表达式树 以前没听过表达式树,只听过表达式,而且听过Lambda表达式,首先介绍一下.NET里表达式树的核心概念:讲代码作为数据,他将一些代码表示为一个对象树,树中的每个节点本身都是一个表达式,不同的 ...
- oracle表复制
1. 复制表结构及其数据: create table table_name_new as select * from table_name_old 2. 只复制表结构: crea ...