- (void)registerForKeyboardNotifications
{ //使用NSNotificationCenter 键盘出现时
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillShow:) name:UIKeyboardDidShowNotification object:nil];
//使用NSNotificationCenter 键盘隐藏时
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillHide:) name:UIKeyboardWillHideNotification object:nil];
}
//当键盘出现或改变时调用 - (void)keyboardWillShow:(NSNotification *)aNotification
{ //获取键盘的高度
NSDictionary *userInfo = [aNotification userInfo];
NSValue *aValue = [userInfo objectForKey:UIKeyboardFrameEndUserInfoKey];
CGRect keyboardRect = [aValue CGRectValue];
int height = keyboardRect.size.height;
NSLog(@”键盘高度%d”,height);
[UIView animateWithDuration:0.5 animations:^{
CGRect subtab = subTable.frame;
subtab.size.height-=height;
subTable.frame = subtab;
} completion:^(BOOL finished) {
NSLog(@”执行动画完毕”);
}];
}
//当键退出时调用 - (void)keyboardWillHide:(NSNotification *)aNotification
{ //获取键盘的高度
NSDictionary *userInfo = [aNotification userInfo];
NSValue *aValue = [userInfo objectForKey:UIKeyboardFrameEndUserInfoKey];
CGRect keyboardRect = [aValue CGRectValue];
int height = keyboardRect.size.height;
NSLog(@”键盘高度%d”,height);
CGRect subtab = subTable.frame;
subtab.size.height+=height;
subTable.frame = subtab;
}
获取键盘高度
最新推荐文章于 2023-04-19 11:24:37 发布