开发的朋友也许会常常遇到点击输入框激活出现键盘而致使输入框被遮住的现象,经常使用的解决方案有两种:ide
一、键盘出现的时候,将内容页面适当上移动画
二、键盘出现的时候,漂浮输入框到适当位置 笔者认为 第一种方案比较简单,这里只介绍第一种,第二种相似。server
1、首先对键盘事件进行监听设置(可写入viewDidLoad中):事件
//增长监听,当键盘出现或改变时收出消息开发
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillShow:) name:UIKeyboardWillShowNotification object:nil];hash
//增长监听,当键退出时收出消息it
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillHide:) name:UIKeyboardWillHideNotification object:nil];io
2、在监听事件的两个方法分别处理视图的上移和下移,移动的距离可根据键盘的高度获得 //实现当键盘出现的时候计算键盘的高度大小。用于输入框显示位置object
- (void)keyboardWillShow:(NSNotification*)aNotification { NSDictionary* info = [aNotification userInfo];select
//kbSize即為鍵盤尺寸 (有width, height)
CGSize kbSize = [[info objectForKey:UIKeyboardFrameEndUserInfoKey] CGRectValue].size;
//获得鍵盤的高度
keyboardhight=kbSize.height;
//将UISCROLLVIEW上移动 hashKeyBoard=YES;
//设置动画的名字
[UIView beginAnimations:@"AnimationOpen" context:nil];
//设置动画的间隔时间 [UIView setAnimationDuration:0.20];
//??使用当前正在运行的状态开始下一段动画
[UIView setAnimationBeginsFromCurrentState: YES];
//设置视图移动的位移
self.view.frame = CGRectMake(self.view.frame.origin.x, self.view.frame.origin.y - keyboardhight, self.view.frame.size.width, self.view.frame.size.height);
//设置动画结束 [UIView commitAnimations]; }
//下移就不赘述咯