在页面弹出键盘时,页面的内容有可能会被软键盘挡住,从而阻碍用户操作,这个问题的可以通过计算弹出的软键盘的高度,让页面向上滑动相应的距离来解决。
获取软键盘高度
-(CGFloat)keyboardHeight:(NSNotification*)paramNotification{
NSValue *keyboardRectAsObject= [[paramNotification userInfo]objectForKey:UIKeyboardFrameEndUserInfoKey];
CGRect keyboardRect;
[keyboardRectAsObject getValue:keyboardRect];
return keyboardRect.size.height;
}
调用方法:
1.首先注册键盘监听事件:键盘显示事件,键盘隐藏事件
[[NSNotificationCenter defaultCenter]addObserver:self
selector:@selector(keyboardWillShow:)
name:UIKeyboardWillShowNotification
object:nil];
[[NSNotificationCenter defaultCenter]addObserver:self
selector:@selector(keyboardWillHidden:)
name:UIKeyboardWillHideNotification
object:nil];
2.监听到事件之后,在方法中调用获取键盘高度的方法,从而获取键盘的高度
-(void)keyboardWillShow:(NSNotification*)paramNotification
{
CGFloat keyboardHeight=[self keyboardHeight:paramNotification];
/*
* 获取键盘高度后处理相应操作
*/
}