避免键盘挡住输入框常用的两个通知方法
2016-08-23 15:28
405 查看
两个监听键盘显示和消失的通知
当键盘状态改变的时候,键盘位置调整(或键盘所在的父view位置调整)
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillShow:) name:UIKeyboardWillShowNotification object:nil]; [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillHide:) name:UIKeyboardWillHideNotification object:nil];
当键盘状态改变的时候,键盘位置调整(或键盘所在的父view位置调整)
#pragma mark - keyboard -(void) keyboardWillShow:(NSNotification *)note { // get keyboard size and loctaion CGRect keyboardBounds; [[note.userInfo valueForKey:UIKeyboardFrameEndUserInfoKey] getValue: &keyboardBounds]; NSNumber *duration = [note.userInfo objectForKey:UIKeyboardAnimationDurationUserInfoKey]; NSNumber *curve = [note.userInfo objectForKey:UIKeyboardAnimationCurveUserInfoKey]; // Need to translate the bounds to account for rotation. keyboardBounds = [self convertRect:keyboardBounds toView:nil]; // get a rect for the textView frame CGRect containerFrame = _contentView.frame; containerFrame.origin.y = CGRectGetHeight(_backgroundMarkView.frame) - (keyboardBounds.size.height + containerFrame.size.height); // animations settings [UIView beginAnimations:nil context:NULL]; [UIView setAnimationBeginsFromCurrentState:YES]; [UIView setAnimationDuration:[duration doubleValue]]; [UIView setAnimationCurve:[curve intValue]]; // set views with new info _contentView.frame = containerFrame; // commit animations [UIView commitAnimations]; } -(void) keyboardWillHide:(NSNotification *)note { NSNumber *duration = [note.userInfo objectForKey:UIKeyboardAnimationDurationUserInfoKey]; NSNumber *curve = [note.userInfo objectForKey:UIKeyboardAnimationCurveUserInfoKey]; // animations settings [UIView beginAnimations:nil context:NULL]; [UIView setAnimationBeginsFromCurrentState:YES]; [UIView setAnimationDuration:[duration doubleValue]]; [UIView setAnimationCurve:[curve intValue]]; // set views with new info _contentView.center = _backgroundMarkView.center; // commit animations [UIView commitAnimations]; }
相关文章推荐
- error while crawling /home/$USER: boost::filesystem::status: Permission denied: “/home$USER/.gvfs”
- canvas.drawArc 起始角度
- Android中的ACCESS_MOCK_LOCATION权限使用Demo
- Linux LVM磁盘管理及LVM扩容
- 有哪些公司有外号,像企鹅,数字公司?
- blender 操作快捷键
- 传智168期JavaEE就业班 day03-js
- strtol,strtoll,strtoul, strtoull字符串转化成数字
- 使用DATE命令修改AIX系统时间
- ListView和CheckBox的混乱问题
- 3.1 mxc_v4l2_capture.c分析---probe函数分析
- 在Java代码中控制视图样式是一个相当不合理的选择
- insert into 的加强版replace into和 insert ignore into
- SDUT OJ 3376 数据结构实验之查找四:二分查找
- HFSS画图中的sweep功能
- 关于cocos2d中的世界坐标与局部坐标
- 一张利用虚拟函数构造的生命周期图
- HDU 5363 Key Set【快速幂取模】
- React第一次涉坑
- 分享到微信微博空间等第三方平台的JS代码