//添加键盘通知
[[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(keyboardWillAppear:) name:UIKeyboardWillShowNotification object:nil];
[[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(keyboardWillDisappear:) name:UIKeyboardWillHideNotification object:nil];
#pragma mark - 私有方法
//其次键盘的高度计算:
-(CGFloat)keyboardEndingFrameHeight:(NSDictionary *)userInfo//计算键盘的高度
{
CGRect keyboardEndingUncorrectedFrame = [[userInfo objectForKey:UIKeyboardFrameEndUserInfoKey]CGRectValue];
CGRect keyboardEndingFrame = [self.view convertRect:keyboardEndingUncorrectedFrame fromView:nil];
return keyboardEndingFrame.size.height;
}
//键盘将要弹出时,改变高度
-(void)keyboardWillAppear:(NSNotification *)notification
{
CGRect currentFrame = self.tableView.frame;
CGFloat change = [self keyboardEndingFrameHeight:[notification userInfo]];
currentFrame.origin.y = currentFrame.origin.y - change;
self.tableView.frame = currentFrame;
}
//最后,当键盘消失后,视图需要恢复原状。
-(void)keyboardWillDisappear:(NSNotification *)notification
{
CGRect currentFrame = self.tableView.frame;
CGFloat change = [self keyboardEndingFrameHeight:[notification userInfo]];
currentFrame.origin.y = currentFrame.origin.y + change;
self.tableView.frame = currentFrame;
}
iOS中改变键盘的高度 监听键盘
最新推荐文章于 2020-12-19 12:59:42 发布