美文网首页
2021-11-09 ios 监听键盘的弹出通知

2021-11-09 ios 监听键盘的弹出通知

作者: cc帅气的昵称 | 来源:发表于2021-11-09 09:53 被阅读0次

// 取消页面自动上移

[IQKeyboardManager sharedManager].enable = NO; //  有第三方IQKeyboardManager的项目

#1.注册监听

[[NSNotificationCenter defaultCenter]addObserver:selfselector:@selector(keyBoardWillShow:)name:UIKeyboardWillShowNotification object:nil];[[NSNotificationCenter defaultCenter]addObserver:selfselector:@selector(keyBoardWillHide:)name:UIKeyboardWillHideNotification object:nil];

#2.根据监听获取键盘的高度

-(void)keyBoardWillShow:(NSNotification*)note{

// 获取用户信息

NSDictionary*userInfo=[NSDictionary dictionaryWithDictionary:note.userInfo];

// 获取键盘高度

CGRect keyBoardBounds=[[userInfo objectForKey:UIKeyboardFrameEndUserInfoKey]CGRectValue];

CGFloat keyBoardHeight=keyBoardBounds.size.height;

// 获取键盘动画时间

CGFloat animationTime=[[userInfo objectForKey:UIKeyboardAnimationDurationUserInfoKey]floatValue];

}

//键盘收起-(void)keyBoardWillHide:(NSNotification*)note{}

#3.记得移除监听!

[[NSNotificationCenter defaultCenter] removeObserver:self name:UIKeyboardWillShowNotification object:nil];    [[NSNotificationCenter defaultCenter] removeObserver:self name:UIKeyboardWillHideNotification object:nil];

相关文章

网友评论

      本文标题:2021-11-09 ios 监听键盘的弹出通知

      本文链接:https://www.haomeiwen.com/subject/tzdmzltx.html