您的位置:首页 > 其它

键盘的弹出与消失(获得弹出与消失的状态)

2015-10-20 14:41 260 查看
首先需要在viewdidload内使用 如下代码,注册通知,并且使用 选择器方法,指明弹出与消失时候调用的方法

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyBoardWillShow:) name:UIKeyboardWillShowNotification object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyBoardWillHide:) name:UIKeyboardWillHideNotification object:nil];

然后实现keyBoardWillShow:与keyBoardWillHide:方法:

-(void)keyBoardWillShow:(NSNotification * )notification{
if (!isKeyBoardShow) {
NSLog(@"键盘弹出啦");
NSDictionary * userInfo =[notification userInfo];
NSValue * value =[userInfo objectForKey:UIKeyboardFrameEndUserInfoKey];
CGRect keyBoardRect=[value CGRectValue];
int height =keyBoardRect.size.height;
self.view.frame=CGRectMake(0,0,self.view.frame.size.width, self.view.frame.size.height-height);
self.tableView.scrollEnabled=YES;
isKeyBoardShow=YES;
}

}

-(void)keyBoardWillHide:(NSNotification * )notification{
if (isKeyBoardShow) {
NSLog(@"键盘消失啦");
// _user.name=self.getName.text;
self.tableView.scrollEnabled=NO;
NSDictionary * userInfo =[notification userInfo];
NSValue * value =[userInfo objectForKey:UIKeyboardFrameBeginUserInfoKey];
CGRect keyBoardRect=[value CGRectValue];
int height =keyBoardRect.size.height;
self.view.frame=CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height+height);
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: