输入框被键盘遮挡时 让整个view上移
2016-08-12 16:14
393 查看
//监听键盘响应事件 [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillShow:) name:UIKeyboardWillShowNotification object:nil]; [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillHide:) name:UIKeyboardWillHideNotification object:nil];
#pragma mark - 键盘显示事件 -(void)keyboardWillShow:(NSNotification *)notification { //获取键盘高度,在不同设备上,以及中英文下是不同的 CGFloat kbHeight = [[notification.userInfo objectForKey:UIKeyboardFrameEndUserInfoKey] CGRectValue].size.height; CGFloat originY = 0;//初始化,顶端的输入框不受键盘影响 if (_RedTextField.editing) { originY = _RedTextField.frame.origin.y; }else if (_GreenTextField.editing){ originY = _GreenTextField.frame.origin.y; }else if (_BlueTextField.editing){ originY = _BlueTextField.frame.origin.y; }else if (_WhiteTextField.editing){ originY = _WhiteTextField.frame.origin.y; } //计算出键盘顶端到TextField底端的距离(加上自定义的缓冲距离INTERVAL_KEYBOARD) CGFloat offset = (originY + _WhiteTextField.frame.size.height + INTERVAL_KEYBOARD) - (self.view.frame.size.height - kbHeight); //取得键盘的动画时间,这样可以在视图上移的时候更连贯 double duration = [[notification.userInfo objectForKey:UIKeyboardAnimationDurationUserInfoKey] doubleValue]; //将视图上移计算好的偏移 if(offset > 0) { [UIView animateWithDuration:duration animations:^{ self.view.frame = CGRectMake(0.0f, -offset, self.view.frame.size.width, self.view.frame.size.height); }]; } } #pragma mark - 键盘消失事件 - (void)keyboardWillHide:(NSNotification *)notify { //键盘动画时间 double duration = [[notify.userInfo objectForKey:UIKeyboardAnimationDurationUserInfoKey] doubleValue]; //视图下沉恢复原状 [UIView animateWithDuration:duration animations:^{ self.view.frame = CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height); }]; }
转载自http://www.cocoachina.com/bbs/read.php?tid=317353
相关文章推荐
- Kafka_2.10-0.10.0.0安装配置文档
- BZOJ3387 [Usaco2004 Dec]Fence Obstacle Course栅栏行动
- Paint 的一个小知识点
- 第四章:Linear Models for Classification exercise 1-9
- Common Subsequence(lcs)
- 求出任何一维整型数组中的第二大的元素值
- arcengine打开cad文件的几种方法
- debian webmin 安装
- Ubuntu 安装谷歌拼音输入法
- iOS Core Bluetooth 蓝牙
- 经典dp模型的变形(未完待续。。)
- Elasticsearch2.3.4集群安装指南
- GCC 编译使用动态链接库
- 关于编译gdk
- 安卓中碰到的JAVA引用传递问题
- 【从零开始写一个简单的ImageLoader框架】MyImageLoader代码简介
- mongodb--update高级用法
- 源码分析-java-Vector、Stack-与ArrayList的相关性
- unity3d项目学习网站%Highcharts中文网站js&实现input输入框模糊查询
- 11.其他复杂数据类型