首先,在viewController里面注册监听
//获取键盘的高度
//键盘出现时的监听
[[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(keyboardWillShow:) name:UIKeyboardWillShowNotification object:nil];
//键盘退出时的监听
[[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(keyboardWillHide:) name:UIKeyboardWillHideNotification object:nil];
然后就可以获取到键盘的高度了
- (void)keyboardWillShow:(NSNotification *)aNotification
{
NSDictionary * userInfo = [aNotification userInfo];
NSValue * value = [userInfo objectForKey:UIKeyboardFrameEndUserInfoKey];
CGRect keyboardRect = [value CGRectValue];
int height = keyboardRect.size.height;
NSLog(@"the keyboard hight is %d",height);}
- (void)keyboardWillHide:(NSNotification *)aNotification
{
}