监听当键盘将要出现时
OC版
[[NSNotificationCenter defaultCenter]addObserver:selfselector:@selector(keyboardWillShow:)name:UIKeyboardWillShowNotification object:nil];
[[NSNotificationCenter defaultCenter]addObserver:selfselector:@selector(keyboardWillHide:)name:UIKeyboardWillHideNotification object:nil];
- (void)keyboardWillShow:(NSNotification *)notification
{
// //获取键盘的高度
// NSDictionary *userInfo = [notification userInfo];
// NSValue *value = [userInfo objectForKey:UIKeyboardFrameBeginUserInfoKey];
// CGRect keyboardRect = [value CGRectValue];
// int height = keyboardRect.size.height;
CGFloatcurkeyBoardHeight = [[[notificationuserInfo]objectForKey:@"UIKeyboardBoundsUserInfoKey"]CGRectValue].size.height;
CGRectbegin = [[[notificationuserInfo]objectForKey:@"UIKeyboardFrameBeginUserInfoKey"]CGRectValue];
CGRectend = [[[notificationuserInfo]objectForKey:@"UIKeyboardFrameEndUserInfoKey"]CGRectValue];
// 第三方键盘回调三次问题,监听仅执行最后一次
if(begin.size.height>0&& (begin.origin.y-end.origin.y>0)){
CGFloatkeyBoardHeight = curkeyBoardHeight;
NSLog(@"第三次:%f",keyBoardHeight);
[UIView animateWithDuration:0.05 animations:^{
self.countLb_bottomH.constant= keyBoardHeight+10*sizeScale;
}];
}
}
- (void)keyboardWillHide:(NSNotification*)notificationswift版
{
//获取键盘的高度
NSDictionary*userInfo = [notificationuserInfo];
NSValue *value = [userInfo objectForKey:UIKeyboardFrameEndUserInfoKey];
CGRectkeyboardRect = [valueCGRectValue];
intheight = keyboardRect.size.height;
self.countLb_bottomH.constant = 12*sizeScale;
}
NotificationCenter.default.addObserver(self,selector:#selector(keyBoardShow(noty:)),name:Notification.Name.UIKeyboardWillShow,object:nil)
NotificationCenter.default.addObserver(self,selector:#selector(keyBoardHidden(noty:)),name:Notification.Name.UIKeyboardWillHide,object:nil)
@objcfunckeyBoardShow(noty:Notification){guardletuserInfo=noty.userInfoelse{return}letvalue=userInfo["UIKeyboardFrameBeginUserInfoKey"]as!NSValueletkeyboardRect=value.cgRectValueletkeyboradHeight=keyboardRect.size.height}
@objcfunckeyBoardShow(noty:Notification){guardletuserInfo=noty.userInfoelse{return}letvalue=userInfo["UIKeyboardFrameEndUserInfoKey"]as!NSValueletkeyboardRect=value.cgRectValueletkeyboradHeight=keyboardRect.size.height}
参考:https://www.jianshu.com/p/06439695716b
网友评论